Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

angular - Angular2: Configuration 'name' conflicts with existing route 'name'

I have some components of which one with dynamic routes.

the dynamic routes are added as,

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. enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...