Vue延迟加载无法正常工作

时间:2018-08-10 22:38:41

标签: webpack vue.js lazy-loading

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

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

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

sudo jupyterhub --port=8080

这是我的webpack.config.js

[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.

这是用于初始化Vue的index.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()],
};

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

1 个答案:

答案 0 :(得分:0)

我不确定您的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
    }
});