How do I add a global scss file in Vue.JS that will compile?

前端 未结 5 1864
无人及你
无人及你 2021-01-13 15:07

I am trying to import a scss file within my VueJS project, where it will then compile and thus be able to use with my project. However, I need some help, as at present it si

5条回答
  •  北海茫月
    2021-01-13 15:37

    to make it simple just use this trick

    1. create a file called vue.config.js (if it was not auto-created by vue-cli when you were creating your project)
    2. add these code to your created file
    module.exports = {
        css: {
          loaderOptions: {
            sass: {
                prependData: `
                @import "@/assets/scss/main.scss";
                `
            }
          }
        }
      };
    
    1. the @ means src dir this means that @/assets is the same as src/assets
    2. I assume that you have the main.scss file, where you include all of you sub scss files if you want to know how it is made, take a look here it is really cool to work with it
    3. prependData may not work or throw an error the reason why I used these is because I am using "sass-loader": "^8.0.2". here are some useful information
    • on "sass-loader": "^7.*.*" try to use data
    • on "sass-loader": "^8.0.2" try to use prependData
    • on "sass-loader": "^9.0.0" try to use additionalData
    1. after that stop server and run it again to load new configurations we made in vue.config.js
    2. if you are working with vuetify it may throw an error, so make sure that you match the processor name with the extension, like if you have .scss files you will need to use these configs
            scss: { //the change was made here (match the option name with file extension)
                prependData: `
                @import "@/assets/scss/main.scss";
                `
            }
    

    if you are using .sass file use these configs

            sass: { //the change was made here (match the option name with file extension)
                prependData: `
                @import "@/assets/scss/main.sass";
                `
            }
    

提交回复
热议问题