Angular2:配置“名称”与现有航线的名字'冲突 [英] Angular2: Configuration 'name' conflicts with existing route 'name'

查看:145
本文介绍了Angular2:配置“名称”与现有航线的名字'冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与动态路由之一某些组件。

I have some components of which one with dynamic routes.

动态路由加入,

let config = [];
for(let i = 0; i < this.pages.length; i++) {
  config.push({
    path: this.pages[i].slug, 
    name : this.pages[i].name, 
    component: AnotherComponent,
  });
}
router.config(config);

每当与动态路由组件被加载一次以上,所述路由再次配置,并且配置冲突的结果。

whenever the component with dynamic route is loaded more than once, the routes are configured again and that results in configuration conflict.

这里的普拉克,动态路由装入链接用户,再次点击用户从用户导航离开后会记录如下所示的错误控制台。

Here's the plunk, The dynamic routes are loaded with the link users, clicking again on users after navigating away from user logs the below shown error to the console.

如何解决呢?感谢您寻找到这一点。

How do I fix it? Thank you for looking into this.

推荐答案

检查路径前加入存在,它

Check if the route exists before adding it

for(let i = 0; i < this.pages.length; i++) {
  if(!router.registry.hasRoute(this.pages[i].name, UsersComponent)) {
    config.push({
      path: this.pages[i].slug, 
      name : this.pages[i].name, 
      component: PersonComponent,
      data : {
        name : this.pages[i].name,
        slug : this.pages[i].slug
      }
    });
  }
}

Plunker

这篇关于Angular2:配置“名称”与现有航线的名字'冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆