您好,登錄后才能下訂單哦!
在Angular中,動(dòng)態(tài)路由可以通過(guò)路由參數(shù)來(lái)實(shí)現(xiàn)??梢栽诙x路由時(shí)使用冒號(hào)(:)來(lái)定義參數(shù),然后在組件中通過(guò) ActivatedRoute 服務(wù)來(lái)獲取參數(shù)的值。
例如,定義一個(gè)動(dòng)態(tài)路由:
const routes: Routes = [
{ path: 'detail/:id', component: DetailComponent }
];
然后在 DetailComponent 中獲取路由參數(shù)的值:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-detail',
templateUrl: './detail.component.html',
styleUrls: ['./detail.component.css']
})
export class DetailComponent implements OnInit {
id: string;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.id = this.route.snapshot.paramMap.get('id');
}
}
在模板中顯示參數(shù)的值:
<p>Detail ID: {{ id }}</p>
這樣就可以根據(jù)動(dòng)態(tài)的路由參數(shù)來(lái)展示不同的內(nèi)容。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。