【问题标题】:angular2 dynamic routes run time component importangular2 动态路由运行时组件导入
【发布时间】:2016-08-09 15:44:27
【问题描述】:

Angular 2 rc.4

我在 routes 变量中定义了动态路由 JSON。我只有组件的问题。我仍然必须使用 import 语句导入所有组件并将它们放在文件顶部才能使用它们,这实际上破坏了动态路由的目的。

我已经看到了一些例子,比如人们尝试使用 AsyncRoute 的 System.import('component_file_name') 已被弃用。任何解决方案建议都提前表示赞赏

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES, Router, RouterConfig } from '@angular/router';
import {IncComponent} from './inc.component';
import {AdditionComponent} from './addition.component';
import {RomanComponent} from './roman.component';
declare var System:any;

import {AjaxRequestService} from './ajax.req.service';

@Component({
    selector: 'app',
    templateUrl:'app/app.html',
    providers: [
        AjaxRequestService,
    ],
    directives: [ROUTER_DIRECTIVES]
})

export class AppComponent {

    constructor(private router: Router){

    let routes: RouterConfig = [
            {
                path: 'increment',
                component: IncComponent
            },{
                path: 'adder',
                component: AdditionComponent
            },{
                path: 'roman',
                component: RomanComponent
            }

        ];

        router.resetConfig(routes);
    }

    title = 'Angular 2 Demo Applications';
}

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    你可以用loadChildren点赞

    { path: 'heroes', loadChildren: 'app/hero/hero.module' }
    

    另见angular2 rc.4 layz routing

    【讨论】:

    • router.resetConfig(routeConfigArr);它需要 routeConfig Array 而 roueConfig 没有属性 loadChildren,我将如何工作
    • RouteConfig 没有,但路由添加到RouteConfig
    • 对不起,我没明白,请您详细说明一下
    • 我的答案中的代码是一条路线。路由配置由此类路由列表组成,并且路由确实具有loadChildren
    • 基于儿童的路线架构不能解决我的问题。
    【解决方案2】:

    我有rc5版本与你分享: http://plnkr.co/edit/18CZVF?p=preview

    使用AppModule后,loadChildren还是一样:

    { path: 'heroes', loadChildren: 'app/hero/hero.module' }
    

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2016-10-04
      • 2020-07-30
      • 2023-02-02
      相关资源
      最近更新 更多