vue-loader加載不上怎么解決?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
vue-loader 是要依賴 vue 的,所以先下載 vue
npm i vue -S
cnpm i vue-loader vue-template-compiler -D
webpack.config.js 中配置相關信息 注意:這里配置好了
再給大家看一下 我的 package.json 文件
注意:
css-loader style-loader 等等 我都已經下載配置完成
{ "name": "webpack-study", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --open --port 3000 --contentBase src --hot" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.26.3", "babel-loader": "^7.1.5", "babel-plugin-transform-runtime": "^6.23.0", "babel-preset-env": "^1.7.0", "babel-preset-stage-0": "^6.24.1", "css-loader": "^1.0.0", "file-loader": "^2.0.0", "html-webpack-plugin": "^3.2.0", "less": "^3.8.1", "less-loader": "^4.1.0", "node-sass": "^4.9.3", "sass-loader": "^7.1.0", "style-loader": "^0.22.1", "url-loader": "^1.1.1", "vue-loader": "^15.4.1", "vue-template-compiler": "^2.5.17", "webpack": "^4.17.1", "webpack-dev-server": "^3.1.5" }, "dependencies": { "vue": "^2.5.17", "webpack-cli": "^3.1.0" } }
重頭戲來了,即使這樣 運行 : npm run dev
仍然無法識別 vue-loader
錯誤原因:
vue-loader@15.*之后除了必須帶有VueLoaderPlugin
解決方案:
需要使用插件VueLoaderPlugin
在webpack.config.js里用const VueLoaderPlugin = require('vue-loader/lib/plugin')引入,
然后在module.exports對象里添加plugins:[new VueLoaderPlugin()]
再次 運行 項目 就可以啦~~~
補充知識:vue-loader加載不上報錯* ./node_modules/vue-loader/lib/index.js You may need an additional loader to handle
報錯代碼:
ERROR in ./src/login.vue?vue&type=template&id=19e76240& 2:0 Module parse failed: Unexpected token (2:0) File was processed with these loaders: * ./node_modules/vue-loader/lib/index.js You may need an additional loader to handle the result of these loaders. | > <h2>這是使用.vue文件渲染出來的</h2> | | @ ./src/login.vue 1:0-84 10:2-8 11:2-17 30:4-35:6 30:68-35:5 32:16-22 33:25-40 @ ./src/main.js
解決辦法:
const path = require('path'); const htmlWebpackplugin = require('html-webpack-plugin'); const VueLoaderPlugin = require('vue-loader/lib/plugin'); module.exports = { entry: path.join(__dirname,'./src/main.js'), output: { path: path.join(__dirname, './dist'), filename: 'bundle.js' }, plugins: [ new htmlWebpackplugin({ //創建一個在內存中生成的html頁面的插件 template: path.join(__dirname, './src/index.html'), filename: 'index.html' }), new VueLoaderPlugin() ], module: { //這個節點用于配置所有的第三方模塊加載器 rules: [ {test: /\.css$/, use:['style-loader','css-loader']},//配置處理.css文件的第三方處理規則 {test: /\.less$/, use: ["style-loader",'css-loader','less-loader']}, {test: /\.scss$/, use: ["style-loader",'css-loader','sass-loader']}, {test: /\.(jpg|png|gif|bmp|jpeg)$/, use: "url-loader?limit=8000"}, {test: /\.(tff|eot|svg|woff|woff2)$/, use: "url-loader"}, {test:/\.js$/, use:'babel-loader',exclude:/node_modules/}, {test: /\.vue$/, use: 'vue-loader'} ] } };
出來需要配置{test: /\.vue$/, use: 'vue-loader'}這個外,還需要載webpack.config.js配置文件中加兩行代碼:
const VueLoaderPlugin = require('vue-loader/lib/plugin');
new VueLoaderPlugin()
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。