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

【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)

Refer to all the solutions in https://github.com/JeffreyWay/laravel-mix/issues/2064, there is no modification function

Failure scheme:

Method 1: Delete the style tag and its content in each vue file. After deleting all the style tags, it still reports an error (failure)

Method 2: Downgrade laravel-mix: ^3.0, the result will be css-loader not found and other errors (failure)

Method 3: The chunk file, which is in the webpack.mix.js file output.chunkFilename: 'js/chunk/[name].js?v=[chunkHash]', still reports an error (failure)

 mix.webpackConfig(
        output: {
    
    
            publicPath: '/', // 不加会报语法错误
            chunkFilename: 'js/chunk/[name].js?v=[chunkHash]' // 分割文件存放位置, 
        }
    })

Method 4: Add asynchronous loading plugins @babel/plugin-syntax-dynamic-import,

Method 4-1 and Method 4-2 are essentially the same, but the configuration method is different.

Method Four-1:

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

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

Method four-2:

# babel.rc
{
    
    
  "presets": [
    "@vue/babel-preset-jsx",
    "@babel/preset-env"
  ],
  "plugins": [
    "@babel/plugin-syntax-dynamic-import"
  ]
}

The dividing line of success

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

Guess you like

Origin blog.csdn.net/qq_22227087/article/details/109517750