Angular2:配置“名称"与现有路由“名称"冲突 [英] Angular2: Configuration 'name' conflicts with existing route 'name'

查看:20
本文介绍了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.

这是

我该如何解决?感谢您对此进行调查.

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天全站免登陆