【Vue Laravel-mix】
报错信息:
Error with Vue lazy loading components: “Failed to resolve async component“
uncaught error during route navigation
cann’t xxxxx 'call ’
bootstrap.js(63)
参考https://github.com/JeffreyWay/laravel-mix/issues/2064 中所有的解决方法,均没有修改功能
output.chunkFilename: 'js/chunk/[name].js?v=[chunkHash]'
,仍旧报错(失败) mix.webpackConfig(
output: {
publicPath: '/', // 不加会报语法错误
chunkFilename: 'js/chunk/[name].js?v=[chunkHash]' // 分割文件存放位置,
}
})
@babel/plugin-syntax-dynamic-import
,方法四-1与 方法四-2 本质是一样的,只是配置方式不一样哈
npm install -D @babel/plugin-syntax-dynamic-import
# webpack.mix.js
mix.babelConfig({
plugins: [
'@babel/plugin-syntax-dynamic-import'
]
});
# babel.rc
{
"presets": [
"@vue/babel-preset-jsx",
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-syntax-dynamic-import"
]
}
# package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"admin-lte": "^3.0.5",
"axios": "^0.19.2",
"bootstrap": "^4.5.2",
"cross-env": "^5.2.1",
"css-loader": "^1.0.0",
"echarts": "^4.9.0",
"element-ui": "^2.13.2",
"jquery": "^3.5.1",
"js-sha256": "^0.9.0",
"laravel-mix": "^5.0.7",
"lodash": "^4.17.20",
"resolve-url-loader": "^3.1.0",
"sass": "^1.29.0",
"sass-loader": "^8.0.2",
"vue": "^2.6.12",
"vue-clipboard2": "^0.3.1",
"vue-fullscreen": "^2.1.6",
"vue-loader": "^15.9.3",
"vue-router": "^3.4.3",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.6.12"
}
}
# webpack.mix.js
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.version()
.webpackConfig({
externals: {
'vue': 'Vue', //这些是你不需要webpakc帮你打包的库
'vue-router': 'VueRouter',
'element-ui': 'ELEMENT', //这个比较坑 一开始以为是ElementUI结果就报错了,
},
output: {
publicPath: '/', // 不加会报语法错误
chunkFilename: 'js/chunk/[name].js?v=[chunkHash]' // 分割文件存放位置
}
}).sourceMaps();
# 父组件:
<template>
<child></child>
</template>
<script>
报错方式:
import child from "../components/child";
export default {
components: {
child,
},
}
运行正常方式:
export default {
components: {
child: ()=>import("../components/child"),
},
}
</script>