How to dynamically introduce css files into vue components?

problem description

if the description of the tag is not accurate, please forgive me

my requirement is to develop multiple sets of skin. When packing, only one of the skins is loaded (there is no need for dynamic skin replacement), because the css file is too large, and there are too many css files for later support. If you write it together, the project is very large, and it is impossible to copy multiple sets of code. It is too painful to maintain (js is consistent)

.

the environmental background of the problems and what methods you have tried

what I am thinking about is to define a variable globally, which can be used to load css, and maintain the corresponding css file. I wonder if it can be realized. If you have a better way, please let me know, thank you very much!

related codes

staticEnv.js
export const CSS = "a";
.vue component
<template>

</template>

<script>
  import {CSS}  from "src/config/staticEnv"
  export default {
    name: "css",
    data() {
      return {
      
      }
    }
  }
</script>


<style scoped lang="stylus" type="text/stylus">
  @import "CSS.styl"

</style>

clipboard.png

single page development, there are many components, I want to put the css under the corresponding component, and then load dynamically. If the variable is a, then I just need to load a.styl

.

what result do you expect? What is the error message actually seen?

if my description is not accurate enough, you are welcome to ask in your reply. I will reply as soon as possible. Thank you again

Apr.02,2021

I have an idea. You can dynamically write your style file into HTML and do it through the link tag. For example, if you want to load a.css, write a tag

.
<link rel="stylesheet" href="a.css">

you can write a method, pass in the name of the css you want to load, and then document.head.appendChild (document.createElement ('link'). Href= "stylesheet name (address)")

< hr >
components dynamically introduce different css

first of all, you can get the environment variable when webpack is compiled. How to define the blog
if you are using the project generated by vue-cli scaffolding, there should be two files to set the vue environment variable. You add an A
clipboard.png
buildwebpack.base.conf.js
clipboard.png
css



npm run devprocess.env.Awebpack.base.conf.jsprocess.env.A = xxx


ovensi

webpack.base.conf.js alias 'theme': ./${process.env.THEME}


https://segmentfault.com/q/10...


config

clipboard.png

clipboard.png


vuestyle


clipboard.png

ok



css
link


vuemountedimport('cssPath')


csscss@font-faceapp.vuestorecreated:

clipboard.png

.
Menu