英文:
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');
英文:
With pure CSS you could use v-bind
, doesn't work with SCSS though:
border-bottom: v-bind(closed ? '3px solid red' : 'inherit');
答案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:
<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>
Then in your SCSS you can setup any style in component nodes in this way:
.mycomponent[isclosed='true']{
/* component style when closed == true */
.header{
/* header style when component closed == true */
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论