【Vue Laravel-mix】Error with Vue lazy loading components: “Failed to resolve async component“

东门子昂
2023-12-01

【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 中所有的解决方法,均没有修改功能

失败方案:

方法一:删除每个vue文件中的style标签及其内容,结果删除所有style标签之后,仍报错(失败)

方法二:降级 laravel-mix: ^3.0 ,结果会产生css-loader 找不到等其他的错误(失败)

方法三:chunk文件,即在 webpack.mix.js 文件中 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 本质是一样的,只是配置方式不一样哈

方法四-1:

npm install -D @babel/plugin-syntax-dynamic-import

# webpack.mix.js
mix.babelConfig({
    plugins: [
        '@babel/plugin-syntax-dynamic-import'
    ]
});

方法四-2:

# 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>

 类似资料: