0

Angular2 version: rc3

Router version: 3.0.0-alfa7

boot.ts:

import { provideRouter, RouterConfig } from '@angular/router';
import {AppComponent} from "./main/app.component"
import {UsersComponent, UserListComponent, UserEditComponent} from "./users/index"

export const routes: RouterConfig = [
    { path: "", redirectTo: "users", terminal: true },
    {
        path: "users",
        component: UsersComponent,
        children: [
            { path: "create", component: UserEditComponent },
            { path: "edit/:id", component: UserEditComponent },
            { path: "", component: UserListComponent }
        ]
    }
];

export const APP_ROUTER_PROVIDERS = [
    provideRouter(routes)
];

When I launch application, router redirects to /users and UserListComponent works just fine.

I have two issues:

  • Navigation to /users/edit/:id from UserListComponent works only through [routerLink] directive (e.g. [routerLink]="['edit', 17]"). But then I try to navigate from UserListComponent using Router itself (e.g. router.navigate(['edit', 17])) then nothing happens;
  • Navigation to /users from it's child components doesn't work any way. I have tried [routerLink]="['/users']", router.navigate(['/users']). Should be noted that [routerLink]="['/users']" renders to a valid anchor tag: <'a href="http://localhost/users"><'/a>
forik
  • 149
  • 4
  • 15

1 Answers1

1

If you navigate a relative path you need to pass the relativeTo parameter:

this.router.navigate(['./edit', 17], {relativeTo: this.currentActivatedRoute});

See also https://github.com/angular/angular/issues/9476

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks, I'll try that. What do you think about a problem with navigating to **/users** from it's child components? – forik Jun 27 '16 at 08:17
  • Sorry, I don't know. You could check if the suggestion from http://stackoverflow.com/questions/38039294/why-doesnt-this-route-match-when-navigating-to-it-directly works in your case as well. – Günter Zöchbauer Jun 27 '16 at 08:31
  • 1
    Ok, I updated to alfa-8 and now able to navigate to parent route. – forik Jun 27 '16 at 20:03