如何在Angular中获取解析中的路线参数值?

问题描述 投票:0回答:1

我已定义解决方案:

export class ItemResolve implements Resolve<number> {
    constructor(private activatedRoute: ActivatedRoute) {}
    resolve(): Promise<number> {
        console.log(this.activatedRoute.snapshot.paramMap); // EMPTY OBJECT
        console.log(this.activatedRoute.snapshot.paramMap.get('id'));  // NULL
        console.log(this.activatedRoute.snapshot.data);  // EMPTY OBJECT
        console.log(this.activatedRoute.snapshot.param);  // EMPTY OBJECT
        return new Promise((resolve) => {
            resolve(0);
        });
    }
}

和路线:

{
    path: 'items/edit/:id',
    component: ItemeditorComponent,
    resolve: {item: ItemResolve}
}

决心,我总是得到空数组,null或未定义的值,即时通讯尝试使用什么都没有关系。我尝试过routeParams,parads,数据和Google认为可用的所有可能的变体。

有人有解决这个问题的想法吗?

我定义了一个解析:导出类ItemResolve实现了Resolve {构造函数(私有ActivateRoute:ActivatedRoute){} resolve():Promise {console ....

angular angular-routing angular-router
1个回答
0
投票

route方法采用ActivatedRouteSnapShot参数,应使用:

interface Resolve<T> {
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T> | Promise<T> | T
}

https://angular.io/api/router/Resolve

© www.soinside.com 2019 - 2024. All rights reserved.