你可以如何根据Vue 3中的布尔值动态更新SCSS属性?

huangapple go评论78阅读模式
英文:

How can I dynamically update a SCSS property based on a boolean value in Vue 3?

问题

我正在使用Vue 3和SASS在我的项目中。我有一个布尔变量:closed,我想在这个布尔变为true时更新我的SCSS中的一个属性。在提供的示例中,当closed为true时,我想在.node类中显示底边框,当closed为false时隐藏它。

在SCSS中是否可以直接实现这一点,还是需要使用JavaScript来控制这种行为?我已经在互联网上搜索了解决方案,但没有找到明确的答案。

任何帮助或指导将不胜感激。谢谢!

英文:

I'm using Vue 3 and SASS in my project. I have a boolean variable : closed and I would like to update a property in my SCSS when this boolean becomes true. In the provided example, I want to display a border-bottom in the .node class when closed is true, and hide it when closed is false.

Is it possible to achieve this directly in SCSS, or do I need to use JavaScript to control this behavior? I've searched on inte for a solution but haven't been able to find a clear answer.

Any help or guidance would be greatly appreciated. Thank you!


<template>
  <div>
    <div class="header" @pointerdown.stop="" @dblclick.stop="" @click="toggleClosed">
      <!-- ... -->
    </div>
    
    <div class="expanded-content" v-show="!closed">
           <!-- ... -->
    </div>
    <div class="reduced-content" v-show="closed">
           <!-- ... -->
    </div>
  </div>
</template>

<script>
import { defineComponent, ref } from "vue";

/* ------------------------ Definition of node logic ------------------------ */
export default defineComponent({
  props: ["data", "emit"],

  setup() {
    const closed = ref(true);

    return {
      closed,
    };
  },

  methods: {
    
    async toggleClosed() {
      this.data.closed = !this.data.closed;
      this.closed = this.data.closed;
    },
  },
  computed: {
    nodeStyles() {
      return {
        width: Number.isFinite(this.data.width) ? `${this.data.width}px` : "",
        height: Number.isFinite(this.data.height) ? `${this.data.height}px` : "",
      };
    },
  },
});
</script>

<style lang="scss" scoped>
@use "sass:math";
@import "../../vars.sass";


.node {
  // ...

  //What I would like to do :
  if (closed) {
  border-bottom: 2px solid $border-color;
  }

 

}
</style>



答案1

得分: 1

使用纯CSS,您可以使用v-bind,但在SCSS中不起作用:

border-bottom: v-bind(closed ? '3px solid red' : 'inherit');

你可以如何根据Vue 3中的布尔值动态更新SCSS属性?

英文:

With pure CSS you could use v-bind, doesn't work with SCSS though:

border-bottom: v-bind(closed ? '3px solid red' : 'inherit');

你可以如何根据Vue 3中的布尔值动态更新SCSS属性?

答案2

得分: 1

在您的模板中,您可以添加一个组件的类名和动态属性(例如isclosed)用于closed的值:

<template>
  <div class="mycomponent" :isclosed="closed">
    <div class="header" @pointerdown.stop="" @dblclick.stop="" @click="toggleClosed">
      <!-- ... -->
    </div>
    
    <div class="expanded-content" v-show="!closed">
      <!-- ... -->
    </div>
    <div class="reduced-content" v-show="closed">
      <!-- ... -->
    </div>
  </div>
</template>

然后,在您的SCSS中,您可以按照以下方式设置组件节点的任何样式:

.mycomponent[isclosed='true']{
   /* 当closed == true时的组件样式 */
   
   .header{
     /* 当组件关闭时的头部样式 */
   }
}
英文:

In your template you can add a component class name and dynamic attribute (eg isclosed) for closed value:

&lt;template&gt;
&lt;div class=&quot;mycomponent&quot; :isclosed=&quot;closed&quot; &gt;
&lt;div class=&quot;header&quot; @pointerdown.stop=&quot;&quot; @dblclick.stop=&quot;&quot; @click=&quot;toggleClosed&quot;&gt;
&lt;!-- ... --&gt;
&lt;/div&gt;
&lt;div class=&quot;expanded-content&quot; v-show=&quot;!closed&quot;&gt;
&lt;!-- ... --&gt;
&lt;/div&gt;
&lt;div class=&quot;reduced-content&quot; v-show=&quot;closed&quot;&gt;
&lt;!-- ... --&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;

Then in your SCSS you can setup any style in component nodes in this way:

.mycomponent[isclosed=&#39;true&#39;]{
/* component style when closed == true */
.header{
/* header style when component closed == true */
}
}

huangapple
  • 本文由 发表于 2023年7月3日 16:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定