Ama*_*mar 1 html parameters routing components angular
我想在按下 list-profile html doc 中提供的链接后发送一个变量。当前代码不起作用并且崩溃了。如果需要,我可以提供更多细节。
列表-profile.component.html
<div class="col col-sm-4 col-sm-offset-4">
<ul id="list_of_users">
<li class="list-group-item list-group-item-action" *ngFor="let user of users"><a [routerLink]="['/profile', user.id]" routerLinkActive="active" (click)="testFunc(user.id)">{{user.name}}</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
app.module.ts
const appRoutes: Routes = [
{ path: 'addProfile', component: AddProfileComponent },
{ path: 'listProfiles', component: ListProfilesComponent},
{ path: 'profile:id', component: ProfileComponent}
];
Run Code Online (Sandbox Code Playgroud)
将路由更改为以下内容
const appRoutes: Routes = [
{ path: 'addProfile', component: AddProfileComponent },
{ path: 'listProfiles', component: ListProfilesComponent},
{ path: 'profile/:id', component: ProfileComponent} // change to this
];
Run Code Online (Sandbox Code Playgroud)
更新以访问值
import {ActivatedRoute} from '@angular/router';
...
constructor(private route:ActivatedRoute){}
ngOnInit(){
this.route.params.subscribe( params =>
console.log(params['id'];
)
}
Run Code Online (Sandbox Code Playgroud)
注意 - 如果您使用的是 Angular (v4),请将 param 更改为 paramMap