Conditional stylesheet in .vue component

流过昼夜 提交于 2021-02-07 13:16:41

问题


I'm working with vue component (cli .vue)

I need to have my stylesheet appear only if certain boolean is true/false. Simplest explanation would be something like : When myVar==false, component is not loading styles.

<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.

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.

Any ideas? Thanks for help or any ideas in advance :)


回答1:


Using SCSS, you can wrap that CSS in a class, something like this:

<style lang="scss">

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

</style>

And then use a Vue class binding for that class:

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

This way the CSS won't apply unless the class is active on that element. If you want the styles to apply all over the app, then put that class on the app root element.



来源:https://stackoverflow.com/questions/47893611/conditional-stylesheet-in-vue-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!