当导入一个没有.vue扩展名的vue文件时,会出现以下错误:
ERROR in ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue Module not found: Error: Can't resolve './components/Navbar'
Webpack配置如下:
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
问题解决办法
方式一
该错误是由于webpack的配置,要使Webpack自动解析.vue扩展,请使用resolve.extensions选项,并包含默认值,如下:
resolve: {
extensions: ['*', '.js', '.vue', '.json']
}
方式二
通过在Webpack解析中包含扩展数组,使Webpack自动解析.vue扩展,如下:
resolve: {
extensions: ['.vue'],
},
评论前必须登录!
注册