API的角度动态路由

时间:2018-05-15 00:40:34

标签: angular angular-routing

使用Angular 6

我需要能够从后端获取一组路由,将它们放入router.config,然后才能导航到获取的路由。

我能够以这种方式添加在typescript文件中定义的路由,并且我能够导航。

const mypaths: Routes = [
    {
        path: 'test',
        component: MyTestComponent
    }
];

this.router.config.unshift(mypaths[0]);
this.router.resetConfig(this.router.config);
this.router.navigateByUrl('test');

上面的作品就像一个魅力。一旦我尝试完全相同的事情,而是从数据库中取出,MyTestComponent会抛出一个错误,询问我是否已将它放入entryComponents(我已经。它没有改变任何东西)。

this.rest.getRoutes('TestMePlease')
    .subscribe(routes => {
        for ( const routePoss in routes ) {
            if ( routes.hasOwnProperty(routePoss) ) {
                for ( let i = 0; i < routes[routePoss].length; ++i ) {
                        this.router.config.unshift(<Route>routes[routePoss][i]);
                    }
                }
            }
            this.router.resetConfig(this.router.config);
            this.router.navigateByUrl('test');

我注意到在获取json之后打印router.config时的输出包含组件的键值(字符串 - 字符串),普通路由器配置打印不会:

{
  "path": "test"
},

VS

{
  "component": "MyTestComponent",
  "path": "test"
},

我是否在使用resetConfig做错了,因为它无法识别我在路由路径组件对中发送的信息?

0 个答案:

没有答案
相关问题