简体   繁体   English

.vue组件中的条件样式表

[英]Conditional stylesheet in .vue component

I'm working with vue component (cli .vue) 我正在使用vue组件(cli .vue)

I need to have my stylesheet appear only if certain boolean is true/false. 只有当某个布尔值为true / false时,我才需要显示样式表。 Simplest explanation would be something like : When myVar==false , component is not loading styles. 最简单的解释是:当myVar == false时 ,组件不加载样式。

<style v-if="myVar" lang="scss"> @import 'mystyles.css' </style>

I know it is impossible in that way, but how I'm able to do 'similar' thing? 我知道这是不可能的,但我怎么能做'类似'的事情?

I need to load my styles in vue if user want to use default Styles, if not I need to prevent them from being loaded. 如果用户想要使用默认样式,我需要在vue中加载我的样式,否则我需要阻止它们被加载。

My component is used not once but many times in page, but that condition of using/not using default css need to be apply by all components as well, so no problem here. 我的组件在页面中不是一次使用而是多次使用,但是使用/不使用默认css的条件也需要适用于所有组件,因此这里没有问题。

Any ideas? 有任何想法吗? Thanks for help or any ideas in advance :) 感谢您的帮助或任何想法提前:)

Using SCSS, you can wrap that CSS in a class, something like this: 使用SCSS,您可以将CSS包装在类中,如下所示:

<style lang="scss">

.conditional-class {
    @import 'stylesheet.scss';
}

</style>

And then use a Vue class binding for that class: 然后为该类使用Vue类绑定:

<div :class="{ conditional-class: true }">
    ...
</div>

This way the CSS won't apply unless the class is active on that element. 这样,除非该类在该元素上处于活动状态,否则CSS将不会应用。 If you want the styles to apply all over the app, then put that class on the app root element. 如果您希望样式应用于整个应用程序,则将该类放在app根元素上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM