Angular2路由器3.0.0-alfa7。无法导航至

时间:2016-06-27 07:50:30

标签: angular angular2-router angular2-router3

Angular2版本:rc3

路由器版本: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)
];

当我启动应用程序时,路由器重定向到 / users ,而 UserListComponent 工作正常。

我有两个问题:

  • UserListComponent 导航至 / users / edit /:id 仅适用于 [routerLink] 指令(例如[routerLink] =&# 34; ['编辑',17]")。但后来我尝试使用路由器本身从 UserListComponent 导航(例如router.navigate([' edit',17]))然后没有任何反应;
  • 从它的子组件导航到 / users 并不会以任何方式工作。我尝试了[routerLink] =" [' / users']",router.navigate([' / users'])。应注意 [routerLink] =" [' / users']" 呈现为有效的锚标记:<' a href = " HTTP://本地主机/用户"><' / A>

1 个答案:

答案 0 :(得分:1)

如果您导航相对路径,则需要传递relativeTo参数:

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

另见https://github.com/angular/angular/issues/9476