vue初学测试之webpack4.29.6集成vue-loader@15.7.0报错

本文介绍了解决Vue-loader在15.*版本后未正确配置插件导致的问题,并提供了一个具体的案例,包括完整的webpack配置和Vue文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错

Module Error (from ./node_modules/vue-loader/lib/index.js): vue-loader was used without the corresponding plugin. Make sure to include VueLo aderPlugin in your webpack config.

百度找到了答案,原来Vue-loader在15.*之后的版本使用时,需要像html-webpack-plugin一样配置插件,在webpack.config.js里配置下面这一句就好了

解决方案:

//webpack.config.js
let VueLoaderPlugin = require('vue-loader/lib/plugin');
new VueLoaderPlugin()  //在plugins里new实例

具体案例代码:执行了npm init -y,完成插件依赖,依次install插件,新建vue文件和入口文件main,如下

//package.json
{
  "name": "newpro",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "dev": "webpack-dev-server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "vue": "^2.6.10",
    "vue-router": "^3.0.2"
  },
  "devDependencies": {
    "css-loader": "^2.1.1",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "vue-loader": "^15.7.0",
    "vue-template-compiler": "^2.6.10",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  },
  "description": ""
}

//App.vue
<template>
    <div>{{msg}}</div>
</template>
<script>
// 结构template 行为script 样式style
export default {
    data(){
        return {
            msg:'hello'
        }
    }
}
</script>

//main.js
import Vue from 'vue';
import App from './App.vue';
new Vue({
    el:"#app",
    render:createElement=>createElement(App)
})

//webpack.config.js
let htmlWebpackPlugin = require('html-webpack-plugin');
let VueLoaderPlugin = require('vue-loader/lib/plugin');
let path = require('path');
module.exports = {
    mode:"development",
    entry:"./src/main.js",
    output:{
        filename:"build.js",
        path:path.resolve('./dist')
    },
    module:{
        rules:[
            {test:/\.css$/,use:['css-loader','style-loader']},//解析css文件
            {test:/\.vue$/,use:'vue-loader'}//解析vue文件,会自动关联vue-template-compiler解析vue模板
        ]
    },
    plugins:[
        new VueLoaderPlugin(),
        new htmlWebpackPlugin({
            template:'./src/index.html'
        })
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值