Angular 4 次页面重新加载或重定向抛出 "loadChunkError":true

Angular 4 page reload or redirect throws "loadChunkError":true

我正在使用 angular 6 MVC 应用程序和 webpack 构建。它在页面加载或重定向时随机抛出 "loadChunkError":true", 错误发生后,angular 路由不工作并不断抛出控制台错误。

[注意:一旦我清除了浏览器缓存,路由就正常了]

Webpack 文件

const webpack = require("webpack");

const path = require("path");

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
var SourceMapDevToolPlugin = require('webpack/lib/SourceMapDevToolPlugin');
let SharedCache = {};
const entryPath = path.resolve(__dirname, "src");
const viewPath = path.resolve(
    __dirname,
    "../Views/Home"
);

const { BaseHrefWebpackPlugin } = require('base-href-webpack-plugin');
const ROOT = path.resolve(__dirname, '');

常量应用程序=${entryPath}; module.exports = 环境选项 => { 环境选项 = 环境选项 || {};

const config = {
    entry: {
        polyfills: `${entryPath}\polyfills.ts`,
        vendors: `${entryPath}\vendor.ts`,
        app: `${entryPath}\main.ts`
    },
    output: {
        path: viewPath,
        publicPath: '/',
        filename: "../../angularBundle/[name].js",
        chunkFilename: '../../angularBundle/[name].js',
        sourceMapFilename: "../../angularBundle/[name].js.map",
    },
    resolve: {
        extensions: [".ts", ".js", ".html"],
        alias: {
            jquery: "jquery/src/jquery"
        }
    },  
module: {
  rules: [
    {
      test: /\.ts$/,
      loaders: ["awesome-typescript-loader?configFileName=./src/tsconfig.json", "angular2-template-loader"]
      },
      {
          test: /\.(ts|js)$/,
          loaders: [
              'angular-router-loader'
          ]
      },
    {
      test: /\.html$/,
      loader: "raw-loader"
    },
    {
      test: /\.css$/,
      loader: "raw-loader"
      }
      ,
      {
          test: /\.(png|jpg|eot|cur|svg|gif|ttf|woff|otf)$/,
          loader: "file-loader",
          options: {
              outputPath: 'fonts/'
          }
      } 
  ]
    },
    devtool: 'eval',
    plugins: [
  new webpack.NoEmitOnErrorsPlugin(),
    new HtmlWebpackPlugin({
        template: viewPath + "/loader.cshtml",
        filename: viewPath + "/Index.cshtml",
        inject: false
        })
    ],
  optimization: {
      splitChunks: { chunks: 'all' },
    },
 };

if (envOptions.MODE === "prod") {
    config.plugins.push(
        new UglifyJsPlugin()
    );
  }

 return config;
};       

错误

Error: Uncaught (in promise): Object: {"loadChunkError":true,"details":{"type":"missing","request"}

请提出解决问题的解决方案或步骤。

谢谢, 维维克

  • 第 1 步:使用 component 而不是 loadChildren 直接加载您的组件然后您可以看到实际错误

  • 第 2 步:修复错误。

  • 第 3 步:像以前一样延迟加载组件。

根本原因: 在我的例子中,由于旧的缓存 js 块,存在加载块错误。
修复:修复了借助哈希生成唯一js的问题

[hash] 是一个 "unique hash generated for every build"

现有
output: { path: viewPath, publicPath: '/', filename: "../../angularBundle/[name].js", chunkFilename: '../../angularBundle/[name].js', sourceMapFilename: "../../angularBundle/[name].js.map", }

已更新[问题已修复]
output: { path: viewPath, publicPath: '/', filename: "../../angularBundle/[name].[hash].js", chunkFilename: '../../angularBundle/[name].[hash].js', sourceMapFilename: "../../angularBundle/[name].[hash].js.map", }