我定义了一个解决方法:
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认为可用的所有可能的变体。
有人有解决这个问题的想法吗?
答案 0 :(得分:1)
route方法带有一个ActivatedRouteSnapShot
参数,您应该使用:
interface Resolve<T> {
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T> | Promise<T> | T
}
https://angular.io/api/router/Resolve
以您的情况
export class ItemResolve implements Resolve<number> {
resolve(route: ActivatedRouteSnapshot): Promise<number> {
const id = route.paramMap.get('id');
return new Promise((resolve) => resolve(0));
}
}