ERROR in ./src/App.vue?vue&type=style&index=0&lang=css& (./node_modules/vue-loader/lib??vue-loade...

目录

  • 出错现象
  • 出错原因
  • 解决方案

出错现象

这个报错是我在用webpack+vue-loader的时候无法解析App.vue文件中<style>css样式导致的错误

  • 源代码
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

  • 报错信息

ERROR in ./src/App.vue?vue&type=style&index=0&lang=css& (./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=style&index=0&lang=css&) 20:0
Module parse failed: Unexpected character '#' (20:0)
File was processed with these loaders:

homework-error2.png

出错原因

因为项目中其他css文件是用less写的,所以这个问题查阅了其他人的方法都没有解决我的问题,后来我看这里默认在vue里面css并没有指定lang,所以应该给css文件进行单独配置。

解决方案

在配置文件中进行配置

module.exports = {
  ...
  module: {
    rules: [{
      test: /\.vue$/,
      use: 'vue-loader'
    },{
      test: /\.less$/,
      use: [
        'style-loader',
        'css-loader',
        'less-loader'
      ]  
    },{
      test: /\.css$/,
      use: [
        'style-loader',
        'css-loader',
      ]
    }]
  },
  plugins: [
    new VueLoaderPlugin()
  ]
}

it works~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。