Webpack你可能需要一个合适的加载器来处理这个文件类型,带有sue

Pak*_*wat 7 webpack vue.js css-loader vue-loader

我使用Webpack创建了我的项目并在开发中使用了Vue.js,但我无法注入<style>Vue模板.

它让我

Module parse failed: Unexpected token (18:5)
You may need an appropriate loader to handle this file type.
| 
| 
> body {
|   background-color: red;
| }
Run Code Online (Sandbox Code Playgroud)

这是我的 webpack.config.js

  ...
  module: {
    rules: [{
        test: /\.vue$/,
        loader: "vue-loader"
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-env"]
        }
      },
    ]
  },
Run Code Online (Sandbox Code Playgroud)

还有我的 App.vue

<template>
  <div id="app">
    <counter></counter>
  </div>
</template>

<script>
import Counter from "./app/Counter.vue";
export default {
  name: "app",
  components: {
    counter: Counter
  }
};
</script>

<style>
body {
  background-color: red;
}
</style>
Run Code Online (Sandbox Code Playgroud)

小智 18

有同样的问题.查看vue-loader css 的文档也需要规则.

安装包vue-style-loadercss-loader

将以下内容添加到您的rules部分webpack.config.js:

  {
    test: /\.css$/,
    use: [
      'vue-style-loader',
      'css-loader'
    ]
  },
Run Code Online (Sandbox Code Playgroud)

  • 我不明白为什么在Typescript,Vue或GitHub / vue-loader域中未对此进行记录。花了很多时间来寻找这种解决方案。 (2认同)