简体   繁体   中英

Conditional stylesheet in .vue component

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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