2017-09-25 5 views
0

list-profile html doc 내에 제공된 링크를 한 번 눌러 변수를 보내려고합니다. 현재 코드가 작동하지 않고 충돌합니다. 필요한 경우 자세한 내용을 제공 할 수 있습니다.routerLink를 통해 새 구성 요소에 매개 변수를 전달하십시오.

리스트 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> 

app.module.ts

const appRoutes: Routes = [ 
{ path: 'addProfile', component: AddProfileComponent }, 
{ path: 'listProfiles', component: ListProfilesComponent}, 
{ path: 'profile:id', component: ProfileComponent} 
]; 

답변

2

변경 다음

const appRoutes: Routes = [ 
    { path: 'addProfile', component: AddProfileComponent }, 
    { path: 'listProfiles', component: ListProfilesComponent}, 
    { path: 'profile/:id', component: ProfileComponent} // change to this 
    ]; 

업데이트에 대한 경로는 값을 액세스하려면

import {ActivatedRoute} from '@angular/router'; 
... 

constructor(private route:ActivatedRoute){} 


ngOnInit(){ 
    this.route.params.subscribe(params => 
     console.log(params['id']; 
    ) 
} 

주 - 각도 (V4)를 사용하는 경우 paramMap하는 PARAM을 변경하십시오

+0

그것은 더 이상 충돌하지 않습니다! 그러나 변수를 가져올 위치는 어디입니까? 생성자 내부에? – Amar

+0

내부 생성자 또는 ngOnInit –

+0

코드 예제를 제공 할 수 있습니까? 때문에 atm 구성 요소 : Class 'ProfileComponent'잘못 인터페이스 'OnInit'구현합니다. 속성 'ngOnInit'의 유형은 적용 할 수 없습니다. '(id : string) =>'void '유형을'() => void 유형으로 지정할 수 없습니다. – Amar