简体   繁体   English

Angular 2(v5)应用程序中的延迟加载模块

[英]Lazy Loading Modules in Angular 2 (v5) Application

I'm having trouble making lazy loading work on an Angular 2 (version 5.1.3) project. 我在使Angular 2(版本5.1.3)项目上的延迟加载工作时遇到麻烦。

I'm following Todd Motto's article on this Lazy Loading Code Splitting , and can't quite make the last mile to get it working. 我正在关注Todd Motto关于此延迟加载代码拆分的文章,并且还不能尽一切努力使它起作用。

I have an app with multiple modules, several of which I want to lazy load (most users don't use the functionality, and the app itself is large with everything bundled in). 我有一个包含多个模块的应用程序,其中一些我想延迟加载(大多数用户不使用该功能,并且应用程序本身很大,捆绑了所有内容)。

The app.module route config looks like this: app.module路由配置如下所示:

export const routes: Routes = [
  { path: '', 
    redirectTo: 'login', 
    pathMatch: 'full'
   },
  { path: 'login', 
    component: LoginComponent
  },
  { path: 'home', 
    component: HomeComponent
  },
  { path: 'reports-engine', 
    loadChildren: './reportsengine/reportsengine.module#ReportsEngineModule'
  },
  { path: '**', 
    component: NotFoundComponent
  }
];

And the ReportsEngine module: 和ReportsEngine模块:

export const routes: Routes = [
  {
    path: '', 
    component: ReportsComponent,
    children: [{ 
      path: '', 
          redirectTo: 'reports-engine',
          pathMatch: 'full'
        },
      { 
      path: 'account', 
          component: Account
        },
      { 
      path: 'accounts-manage', 
          component: AccountsManage
        },
      {
      path: '**', 
          component: NotFoundComponent
      }]
  }
];

My webpack.config (the relevant parts) are here: 我的webpack.config(相关部分)在这里:

output: {
    filename: '[name].js',
    chunkFilename: '[name].chunk.js',
    path: path.resolve(cwd, 'build'),
    publicPath: './build/',
    sourceMapFilename: '[name].map'
},

rules.push({
    test: /\.ts$/, 
        loaders: [
            'awesome-typescript-loader',
            'angular2-template-loader'
        ] ,
    test: /\.(ts|js)$/,
        loaders: [
            'angular-router-loader'
        ] ,
    include: [
        path.resolve(cwd, 'app')
    ]
});

Currently it's only building the main app.js and vendor.js files (and .map files), not the 0.chunk.js etc. files. 目前,它仅构建主要的app.js和vendor.js文件(和.map文件),而不构建0.chunk.js等文件。

And when I navigate to the /reports-engine url, I get a 'page not found', which I'm expecting the ReportsComponent. 当我导航到/ reports-engine URL时,会得到一个“找不到页面”,我期望使用ReportsComponent。

I'm not sure what I'm missing. 我不确定我缺少什么。

ReportEngine Module. ReportEngine模块。

export const routes: Routes = [
  {
    path: '', 
    component: ReportsComponent,
    children: [    
      { 
        path: 'account', 
        component: Account
      },
      { 
        path: 'accounts-manage', 
        component: AccountsManage
      },
      {
        path: '**', 
        component: NotFoundComponent
      }
    ]
  }
];

Change Like this it will work. 更改像这样,它将起作用。

if you want children path:'' ->check put like this 如果你想要孩子的路径:''->像这样检查

 children: [
      {
        path: '',
        component: AboutComponent
      }
 ]

if you have any doubt please visit this link it is simply to do the lazy loading 如果您有任何疑问,请访问此链接,只需执行延迟加载

https://scotch.io/courses/routing-angular-2-applications/lazy-loading https://scotch.io/courses/routing-angular-2-applications/lazy-loading

I hope it will help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM