【问题标题】:Vue Lazy Loading Not WorkingVue延迟加载不起作用
【发布时间】:2019-01-18 13:26:30
【问题描述】:

在我的项目中添加了几个 Vue 组件后,我的 webpack 包最终变得很大,所以我尝试使用基于本教程的延迟加载:

https://vuejsdevelopers.com/2017/07/03/vue-js-code-splitting-webpack/

但是,我不断收到以下错误:

[Vue warn]: Failed to resolve async component: function(){return n.e(0).then(n.bind(null,155)).then(function(t){return t.default})}
Reason: Error: Loading chunk 0 failed.

这是我的 webpack.config.js

  /**
 * Webpack configuration for development
 */
const path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { VueLoaderPlugin } = require('vue-loader');
var inProduction = (process.env.NODE_ENV === 'production');
require("babel-polyfill");

module.exports = {
  devtool: 'source-map',
  entry: ["babel-polyfill", path.join(process.cwd(), 'src/index')],
  output: {
    filename: 'bundle.js',
    path: path.join(process.cwd(), 'public', 'assets'),
    publicPath: 'js/',
    chunkFilename: 'chunks/[name].js',
  },
  resolve: {
    alias: {
      vue: 'vue/dist/vue.js'
    }
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    }),
    new VueLoaderPlugin()
  ],
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js?$/,
        use: [
        {
          loader: "babel-loader",
          options: {
            "presets": [["es2015", {"modules": false}]]
          }
        }
        ],
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      }
    ],
  },
  target: 'web',
  //externals: [nodeExternals()],
};

这里是初始化 Vue 的 index.js 文件:

import Vue from 'vue';

Vue.component('latest-articles', () => import('./javascripts/components/LatestArticles.vue').then(m => m.default));

const app = new Vue({
    el: '#app',
    components: {
        'latest-articles': () => import('./javascripts/components/LatestArticles.vue').then(m => m.default)
    }
});

我想知道我错过了什么。任何帮助,将不胜感激!

【问题讨论】:

    标签: webpack vue.js lazy-loading


    【解决方案1】:

    我不确定您的 webpack 和 vue.js 版本。尝试在 index.js 中编辑代码,如下所示,看看控制台是否有任何错误。

    import Vue from 'vue';
    const LatestArticles = () =>import('./javascripts/components/LatestArticles.vue');
    
    const app = new Vue({
        el: '#app',
        components: {
            'latest-articles': LatestArticles
        }
    });
    

    【讨论】:

    • 我不断收到“ [Vue 警告]:无法解析异步组件:function(){return Promise.all([ne(0),ne(1)]).then(n.bind (null,347))} 原因:错误:加载块 0 失败。"
    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多