Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态添加路由_Javascript_Angular_Routes - Fatal编程技术网

Javascript 动态添加路由

Javascript 动态添加路由,javascript,angular,routes,Javascript,Angular,Routes,我有一个选项卡组件服务,它有一个设置选项卡的方法。每个选项卡都有一个到单独零部件的管线 setTabs(components: any[]) { // components: [{ title: 'Foo bar', path: 'foo-component', component: FooComponent }, // { title: 'Whoo bar', path: 'whoo-component', component: WhooComponent }

我有一个选项卡组件服务,它有一个设置选项卡的方法。每个选项卡都有一个到单独零部件的管线

setTabs(components: any[]) {

        // components: [{ title: 'Foo bar', path: 'foo-component', component: FooComponent },
        // { title: 'Whoo bar', path: 'whoo-component', component: WhooComponent }]

        for (let i = 0; i < components.length; i++) {
            let component = components[i];
            this.addTab(component);
            this._router.config.push({path: component.path, component: component.component});
        }


        // << DOESNT RESET ROUTE, WHY?? >>
        this._router.resetConfig(this._router.config);
    }
setTabs(组件:任意[]){
//组件:[{title:'Foo bar',path:'Foo component',component:Foo component},
//{title:'Whoo bar',path:'Whoo component',component:WhooComponent}]
for(设i=0;i
this.\u router.resetConfig(this.\u router.config);
}

如何动态添加路由?

什么是“不工作”的确切含义?我认为您应该创建路由数组,然后使用
\u router.resetConfig(routes)
而不是
此操作。_router.config.push(…)
我一直收到一个错误消息:未处理的承诺拒绝:无法匹配任何路由。URL段:“用户/某人/展示”;区域:角度;任务:承诺;值:错误:无法匹配任何路由。URL段:“user/someone/showcase”也许您应该首先尝试静态添加路由配置,然后在它们工作时动态添加路由配置。也许您需要
\u router.root.resetConfig(routes)