简体   繁体   English

如何在 Vue Nuxt 项目中导入整个 SCSS 文件夹?

[英]How to import whole SCSS folder in Vue Nuxt project?

At my company we are not writing css in Vue files, we prefer to do it the old way with SCSS .在我的公司,我们没有在 Vue 文件中编写 css,我们更喜欢使用SCSS方式。

Problem is, we end up with a need of writing new import in styles.scss any time we create new component, and it really bugs me in bigger projects.问题是,我们最终需要在styles.scss创建新组件时在styles.scss中编写新的导入,这在更大的项目中确实让我感到styles.scss

Not so long ago, when I have been developing in React, I imported module called node-sass-glob-importer in webpack.config file, tweaked a bit ( you can check here ) and it worked - I could have imported folder like this: @import "components/**";不久前,当我在 React 中进行开发时,我在webpack.config文件中导入了名为node-sass-glob-importer模块, webpack.config调整了一下( 您可以在此处查看)并且它起作用了 - 我可以像这样导入文件夹: @import "components/**";

In Nuxt, I only have nuxt.config.js file and I am lost a bit.在 Nuxt 中,我只有nuxt.config.js文件,我有点迷失了。 I know how to extend some simple stuff there, but this seems to be more complicated.我知道如何在那里扩展一些简单的东西,但这似乎更复杂。

Any help of importing node-sass-glob-importer or doing the same thing in some other way?导入node-sass-glob-importer或以其他方式做同样的事情有什么帮助吗?

how about using https://github.com/nuxt-community/style-resources-module and than:如何使用https://github.com/nuxt-community/style-resources-module然后:

export default {
  modules: ['@nuxtjs/style-resources'],
  styleResources: {
    scss: [
      './assets/yourFolder/*.scss'
    ]
  }
}

You can use node-sass-glob-importer in Nuxt like this:您可以像这样在 Nuxt 中使用node-sass-glob-importer

nuxt.config.js: nuxt.config.js:

const globImporter = require('node-sass-glob-importer');

export default {
  ...
  css: [
    '~/assets/scss/global.scss'
  ],
  ...
  build: {
    extend(config, {loaders: {scss}}) {
      const sassOptions = scss.sassOptions || {};
      sassOptions.importer = globImporter();
      scss.sassOptions = sassOptions;
    }
  }
}

global.scss: global.scss:

@import "./assets/scss/base/*";
@import "./assets/scss/components/*";

styleResources is used for things like variables, mixins and functions that you want to use anywhere in your SCSS without having to import the files. styleResources 用于诸如变量、mixin 和函数之类的东西,您希望在 SCSS 中的任何地方使用,而无需导入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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