我的決心:

我的組件:

我已經嘗試過“this.route.data.subscribe”和“this.route.snapshot.data['objectPost']”,但我仍然可以獲得帖子的資料。有人可以解釋一下我的問題是什么嗎...?感謝:D
uj5u.com熱心網友回復:
關于決議你必須知道的第一件事是當決議的資料準備好時路由會改變。
所以,這是流程:
- 您的路線 ->
localhost:4200/child2。 - 決議器采取行動。
- 您的路線將更改為
localhost:4200/child2/:postID。
因此,當您的決議器采取行動時,路由仍然是localhost:4200/child2,route.params.postID并將回傳undefined。
您可以使用Locationfrom @angular/commonto replaceStateof url 然后導航到child2/:postID. 這樣,您將在決議器中獲得 id。
uj5u.com熱心網友回復:
嘗試通過“paramMap”訪問
@Injectable({ providedIn: 'root' })
export class DemoResolver implements Resolve<any> {
constructor() {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any>|Promise<any>|any {
console.log(route.paramMap.get('postID')); // try with this code
}
}
注意:檢查您是否已在模塊中注冊了決議器,如下例所示:
{
provide: 'DemoResolver',
useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => response
}
更多資訊:https : //angular.io/api/router/Resolve
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370257.html
