Angular 4 Ahead-of-Time - 延迟加载不起作用

标签 angular lazy-loading angular-routing angular2-aot rollup

我有一个 SystemJS 项目。我使用 NGC 和 Rollup 进行 AOT 编译。一切正常,但用于路由的延迟加载不起作用。 比如我的tsconfig-aot.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "baseUrl": ".",
        "paths": {
            "app/*": [ "../src/app/*" ]
        }
  },
  "typeRoots": [
    "node_modules/@types"
  ],
  "files": [
    "../src/app/app.module.aot.ts"
  ],
  "exclude": [
    "node_modules",
    "typings"
  ],
  "angularCompilerOptions": {
    "genDir": "../",
    "skipMetadataEmit": false,
    "skipTemplateCodegen": false
  }
}

rollup.config.app.js

'use strict';

import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs    from 'rollup-plugin-commonjs';
import uglify      from 'rollup-plugin-uglify'

export default {
    entry: './src/app/app.module.aot.js',
    dest:  './src/dist/app.module.min.js',
    sourceMap: true,
    format: 'iife',
    onwarn: function(warning) {
        // Skip certain warnings
        if ( warning.message.indexOf('\'default\' is not exported by rollup') === -1) { return; }
        if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
        if ( warning.code === 'EVAL' ) { return; }
        console.warn( warning.message );
    },
    plugins: [
        nodeResolve({jsnext: true, module: true}),
        commonjs({
            include: [
                './node_modules/rxjs/**'
            ]
        }),
        uglify()
    ]
}

运行带汇总的 AOT 后一切正常,但当我尝试为我的应用程序使用延迟加载时,它不起作用。

const routes: Routes = [
  {
    path: "test/:id",
    loadChildren: "./src/app/app.module#TestModule"
  }
];

AOT 构建通过,没有任何错误,并且在使用 AOT 运行应用程序后,我在开发工具中没有看到任何错误。但延迟加载也不起作用。

UPD 在 JIT 编译中,延迟加载可以正常工作。

知道如何解决这个问题吗?

最佳答案

(只是从我的评论中回答,如果有效 - 请随意投票/接受答案)

您必须使用 webpack 进行 AOT 和延迟加载。 Rollup 将不起作用(至少目前如此)。

使用@ngtools/webpack配置AOT+懒加载

webpack.config.js

const ngToolsWebpack = require('@ngtools/webpack');
var webpack = require('webpack');

module.exports = {
  resolve: {
    extensions: ['.ts', '.js']
  },
  entry: './app/main.aot.ts',
  output: {
    path: './dist',
    publicPath: 'dist/',
    filename: 'app.main.js'
  },
  plugins: [
    new ngToolsWebpack.AotPlugin({
      tsConfigPath: './tsconfig.json'
    }),
        new webpack.LoaderOptionsPlugin({
            minimize: true,
            debug: false
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            output: {
                comments: false
            },
            sourceMap: true
        })
  ],
  module: {
    loaders: [
      { test: /\.scss$/, loaders: ['raw-loader', 'sass-loader'] },
      { test: /\.css$/, loader: 'raw-loader' },
      { test: /\.html$/, loader: 'raw-loader' },
      { test: /\.ts$/, loader: '@ngtools/webpack' }
    ]
  },
  devServer: {
    historyApiFallback: true
  }
};

tsconfig.json

{
  "compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "mapRoot": "",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2015",
      "dom"
    ],
    "outDir": "lib",
    "skipLibCheck": true,
    "rootDir": "."
  },
  "angularCompilerOptions": {
    "genDir": "./app/ngfactory",
    "entryModule": "app/app.module#AppModule"
  }
}

引用:http://www.dzurico.com/angular-aot-webpack-lazy-loading/

关于Angular 4 Ahead-of-Time - 延迟加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288368/

相关文章:

css - 来自 assets 的图像在 scss 中找不到

nhibernate - 如何为整个应用程序禁用 NHibernate 延迟加载?

java - hibernate 延迟获取

javascript - AngularJS Promise 被拒绝而不等待异步响应(它应该解决)

RouteConfigLoadEnd-Handler 中的 Angular 访问 route._loadedConfig

javascript - 在没有海报的情况下预览视频 html5 的缩略图? Angular

angularjs - 从 Angular 1 传递函数以降级 Angular 2 组件

php - Symfony 和 Doctrine : lazy loading is not working

Angular - 根据兄弟 RouterLinkActive 将样式应用于元素?

angular - 在开发中禁用 RayGun