Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Vue, how would I load different CSS files depending on some logic / data?

I'm trying to enable different theming options for my app that users can choose from.

I know that I could switch out a class on the <body> tag, but that would make the CSS far messier than just using two different .scss files with different variable values.

Any ideas?

like image 421
jetlej Avatar asked Nov 13 '18 23:11

jetlej


1 Answers

I can't think of an elegant way to do this. If you're using single file components (SFCs) you can make use of CSS modules. From the vue-loader documentation ..

You can have more than one <style> tags in a single *.vue component. To avoid injected styles to overwrite each other, you can customize the name of the injected computed property by giving the module attribute a value

<style module="a">
  /* identifiers injected as a */
</style>

<style module="b">
  /* identifiers injected as b */
</style>

So you might have a style tag for your common styling, and other style tags for theme specific styling. You can then decide which module/style-tag a class is picked from with a computed property that uses your logic to return the module's name.

like image 90
Husam Ibrahim Avatar answered Oct 19 '22 20:10

Husam Ibrahim