无法配置CSS模块与vue-loader配合使用

问题描述 投票:3回答:1

CSS模块在我通过vue-cli3安装的新vue项目上不起​​作用。这是文档中的两个配置示例。其中一个被我的应用程序忽略,第二个在构建时显示错误

我使用以下组件来测试配置:

<template>
  <div class="hello">
      The red text here
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<style module>
  .hello {
    color: red;
  }
</style>
  1. 第一个文档示例:

https://cli.vuejs.org/guide/css.html#referencing-assets

您可以使用<style module>开箱即用地在* .vue文件中使用CSS模块。

我的vue.config.js

module.exports = {
    css: {
        loaderOptions: {
            css: {
                localIdentName: '[name]-[hash]',
            }
        }
    }
}

文本仍为黑色

  1. 第二个文档示例(下面的链接中的第一个代码段):

https://vue-loader.vuejs.org/guide/css-modules.html

我的vue.config.js

module.exports = {
    configureWebpack: {
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: [
                        'vue-style-loader',
                        {
                            loader: 'css-loader',
                            options: {
                                // enable CSS Modules
                                modules: true,
                                // customize generated class names
                                localIdentName: '[local]_[hash:base64:8]'
                            }
                        }
                    ]
                }
            ]
        }
    }
}

执行npm run serve时出现错误:

Syntax Error: SyntaxError

(5:1) Unknown word

  3 | // load the styles
  4 | var content = require("!!../../node_modules/css-loader/index.js??ref--13-1!../../node_modules/cache-loader/dist/cjs.js??ref--0-0!../../node_modules/vue-loader/lib/index.js??vue-loader-options!./HelloWorld.vue?vue&type=style&index=0&module=true&lang=css&");
> 5 | if(typeof content === 'string') content = [[module.id, content, '']];
    | ^
  6 | if(content.locals) module.exports = content.locals;
  7 | // add the styles to the DOM

我在github上发现了很多有关上一个错误的问题,但没有答案

[请帮助我理解,我做错了。

vue.js vue-cli css-modules vue-loader
1个回答
0
投票

您的第一次尝试几乎可以,只需通过模块标志:

module.exports = {
    css: {
      loaderOptions: {
        module: true, // here is the config
        css: {
          localIdentName: '[name]-[hash]',
        }
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.