Search code examples
angularangular2-routingangular2-components

Angular2 new component object on different url


I have PageComponent which data is renewed on different url (by using PageService). It uses same PageComponent object. I want that PageComponent would be new object on new url.

  • 1st Q: How to create new PageComponent on different url?
  • 2nd Q: Is it good approach if new Component is made on different url?
const routes: Routes = [
    {path: ':uri', component: PageComponent}
];

Solution

  • IF you are using angular 2 routing then there will be an index.html page and the component will be changed depending on the path in the URL.

    For Ex.

    RouterModule.forRoot([
      { path: 'heroes', component: HeroesComponent},
      { path: 'details', component: HeroesDetailComponent}
    ])
    

    It will load HeroesComponent when URL will be /heroes at place of <router-outlet> in index.html page and when URL will be /details then it will load HeroesDetailComponent at the place of <router-outlet>.

    For detail, you can read below URL

    https://angular.io/docs/ts/latest/tutorial/toh-pt5.html