在Angular 4中使用ID进行路由

Jos*_*eph 3 angular2-routing angular angular-router

我在Angular 4中进行路由时需要帮助。我想显示这样的URL。Localhost:4200 / user / 1(如果我单击第一个用户的查看详细信息)。我对如何做到这一点感到困惑。我在下面的代码中已尽力而为,但仍然无法正常工作。

app-routing.module.ts

const appRoutes: Routes = [
    { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
    { path: 'dashboard', component: DashboardComponent },
    { path: 'user', component: UserComponent, children: [

        { path: 'create-new-user', component: CreateNewUserComponent },
        { path: 'user-list', component: UserListComponent },
        { path: 'user-detail/:id', component: UserDetailComponent },

]},
];


@NgModule({
    imports: [RouterModule.forRoot(appRoutes)],
    exports: [RouterModule]
})
export class AppRoutingModule {

}
Run Code Online (Sandbox Code Playgroud)

Jan*_*jas 16

不久前我遇到了这个问题。这里的问题是您拥有的路由配置。您不能转到 /user/1,因为您将 'user-details' 路由定义为 'user' 路由的子级。您要么必须导航到“/user/user-detail/1”,或者只是定义一个新的子路由,路径“:id”指向您的详细信息组件,现在您就可以导航到“/user/1” .

总结:随着路线:

const appRoutes: Routes = [
    ...
    { path: 'user', component: UserComponent, children: [

        ...
        { path: 'user-detail/:id', component: UserDetailComponent },

    ]},
];
Run Code Online (Sandbox Code Playgroud)

您可以导航到“/user/user-details/1”。随着路线:

const appRoutes: Routes = [
    ...
    { path: 'user', component: UserComponent, children: [

        ...
        { path: ':id', component: UserDetailComponent },

    ]},
];
Run Code Online (Sandbox Code Playgroud)

您可以导航到“/user/1”。希望对你有帮助。


bvd*_*vdb 5

我认为您的ViewDetail()方法需要一个索引参数。例如

public ViewDetail(index: number)
{
  this.index = index;
  ...
}
Run Code Online (Sandbox Code Playgroud)

有一个很好的例子,这里的路由本身。他们使用以下路由定义:

const routes: Routes = [
  ...
  { path: 'detail/:id', component: HeroDetailComponent },
  ...
];
Run Code Online (Sandbox Code Playgroud)

至于打字稿代码。

gotoDetail(): void {
  this.router.navigate(['/detail', this.selectedHero.id]);
}
Run Code Online (Sandbox Code Playgroud)

因此,我认为您在正确的轨道上。

我认为只是未设置索引。


Rob*_*ert 5

在您的路由文件中

const appRoutes: Routes = [ 
      { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, 
    { path: 'dashboard', component: DashboardComponent },
   { path: 'user', component: UserComponent, children: [
      { path: 'create-new-user', component: CreateNewUserComponent }, 
     { path: 'user-list', component: UserListComponent },
    { path: ':id', component: UserDetailComponent },
]}
]; 
Run Code Online (Sandbox Code Playgroud)

在您的视图方法中这样做

ViewuserDetail(user_id : any){
   let url: string = "/user/" + user_id
        this.router.navigateByUrl(url);
     }
Run Code Online (Sandbox Code Playgroud)

在您的模板中

<td><button class="btn btn-primary"  (click)="ViewuserDetail(user_object.id)">View</button></td> 
Run Code Online (Sandbox Code Playgroud)


Rah*_*ngh 3

应该是这样的

  ViewDetail(index : any){
    this.router.navigate(['/user-detail', index]);
  }
Run Code Online (Sandbox Code Playgroud)

如果您想要列出的 url,/user/1请更改路由器配置以匹配名称 i:e,而不是user-detail更改为user

更新

是的,将模板行更改为此

<td><button class="btn btn-primary" style="cursor: pointer;" (click)="ViewDetail(i)">View</button></td> // it should be i instead of index
Run Code Online (Sandbox Code Playgroud)