URL 내부 어딘가에 있는 routerLink에 매개 변수를 전달하는 방법은 무엇입니까?
매개 변수를 전달할 수 있습니다.routerLink등의 경로에 대하여
/user/:id
서면으로
[routerLink]="['/user', user.id]"
하지만 다음과 같은 경로는 어떻습니까?
/user/:id/details
이 매개 변수를 설정하는 방법이 있습니까? 아니면 다른 URL 방식을 고려해야 합니까?
특정 예제에서 다음을 수행할 수 있습니다.routerLink:
[routerLink]="['user', user.id, 'details']"
컨트롤러에서 이를 수행하려면 다음과 같이 주입할 수 있습니다.Router및 사용:
router.navigate(['user', user.id, 'details']);
라우팅 및 탐색의 Angular docs Link Parameters Array 섹션에서 자세한 내용
답변이 많이 늦었을 수도 있지만, 파라메타로 다른 페이지를 탐색하고 싶다면,
[routerLink]="['/user', user.id, 'details']"
또한 다음과 같은 라우팅 구성도 잊어서는 안 됩니다.
[path: 'user/:id/details', component:userComponent, pathMatch: 'full']
표의 첫 번째 구성:
const routes: Routes = [
{
path: 'class/:id/enrollment/:guid',
component: ClassEnrollmentComponent
}
];
이제 스크립트 코드 유형:
this.router.navigate([`class/${classId}/enrollment/${4545455}`]);
다른 구성 요소에서 매개 변수 수신
this.activatedRoute.params.subscribe(params => {
let id = params['id'];
let guid = params['guid'];
console.log(`${id},${guid}`);
});
매우 간단합니다.
<a routerLink="/user/{{id}}/details"></a>
이를 달성하는 방법은 여러 가지가 있습니다.
- [routerLink] 지침을 통해
- 라우터 클래스의 탐색(어레이) 방법
- 문자열을 사용하고 약속을 반환하는 navigateByUrl(string) 메서드입니다.
routerLink 특성을 사용하려면 기능 모듈을 느리게 로드한 경우 routingModule을 기능 모듈로 가져오거나 AppModule 가져오기 배열에 자동으로 추가되지 않은 경우 app-routing-module을 가져오기만 하면 됩니다.
- 라우터 링크
<a [routerLink]="['/user', user.id]">John Doe</a>
<a routerLink="urlString">John Doe</a> // urlString is computed in your component
- 탐색
// Inject Router into your component
// Inject ActivatedRoute into your component. This will allow the route to be done related to the current url
this._router.navigate(['user',user.id], {relativeTo: this._activatedRoute})
- 탐색 기준URL
this._router.navigateByUrl(urlString).then((bool) => {}).catch()
constructor(private activatedRoute: ActivatedRoute) {
this.activatedRoute.queryParams.subscribe(params => {
console.log(params['type'])
}); }
이건 나한테 효과가 있어요!
app-routing.module.ts
const routes: Routes = [
{ path: 'products', component: ProductsComponent },
{ path: 'product/:id', component: ProductDetailsComponent },
{ path: '', redirectTo: '/products', pathMatch: 'full' },
];
컨트롤러에서 다음과 같이 탐색할 수 있습니다.
this.router.navigate(['/products', productId]);
다음 경로로 이동합니다. http://localhost:4200/products/product-id
constructor(private activatedRoute: ActivatedRoute) {}
당신이 사용할 경우
`this.activatedRoute.parmas.subscribe(params => { console.log(params['type']) }); }`
url에서 매개 변수를 가져오기 위해 쿼리 매개 변수 대신 soparams -
동적 값을 사용하여 링크를 생성할 수 있습니다.동적 링크의 경우 경로 세그먼트 배열을 전달한 다음 각 세그먼트에 대한 매개 변수를 전달합니다.예를들면,['/team', teamId, 'user', userName, {details: true}]/team/11/user/bob;sys=true에 대한 링크를 생성합니다.
언급URL : https://stackoverflow.com/questions/38062702/how-to-pass-a-parameter-to-routerlink-that-is-somewhere-inside-the-url
'programing' 카테고리의 다른 글
| 인증서 서명 요청을 얻는 방법 (0) | 2023.05.22 |
|---|---|
| 목록: 카운트 대 카운트() (0) | 2023.05.22 |
| 파일의 전체 경로에서 디렉터리를 가져오려면 어떻게 해야 합니까? (0) | 2023.05.22 |
| Excel에서 숫자 형식을 수천(K)으로 지정합니다. (0) | 2023.05.22 |
| 문자열에서 숫자가 아닌 문자 제거 (0) | 2023.05.22 |