gpt4 book ai didi

Angular 2 : How to get params on resolve

转载 作者:太空狗 更新时间:2023-10-29 17:32:34 26 4
gpt4 key购买 nike

溃败策略:

export const routes = [
{
path : 'rto-activation/:id',
component : RtoActivationComponent,
resolve : {
'singleRto': SingleRtoResolve
},
children : [
{path: 'start', component: ActivationStartComponent},
{path: 'warning', component: WarningComponent},
{path: 'confirm', component: ConfirmRtoDetailsComponent},
{path: 'ldWaiver', component: LDWaiverComponent},
{path: 'payment-schedule', component: PaymentScheduleComponent},
{path: 'user-reference', component: ReferenceComponent}

]
}

SingleRtoResolve:

constructor(private router: Router,
private route: ActivatedRoute) {
}

resolve() {
var self = this;
return new Promise((resolve, reject) => {
self.subscription = self.route.params.subscribe(
(param: any) => {
let id = param[ 'id' ];
self.rtoService.getRto(null, +id)
.then((res: any) => {
resolve(res)
})

});
});
}

我知道:我们通常从 ActivatedRoute 服务获取参数。

问题:我可以从路由器服务获取参数吗?

简介:尝试在 Resolve Injectable 上获取路由参数,因为在该阶段路由未激活,我无法获取参数。

用例:当用户使用一些 (:id) 打开任何子路由(隐式和显式)时,数据应该在父路由中解析。

在任何子组件中激活路由时成功获取Params:

ngOnInit() {
let self = this;
this.subscription = this.route.params.subscribe(
(param: any) => {
let id = param[ 'id' ];
self.rtoService.getRto(null, +id)
.then((res: any) => {

})

});
}

最佳答案

你的 SingleRtoResolve应该implements Resolve<T> .然后你只需要你的数据服务(我猜是RtoService)在你的constructor() {} .不需要 RouterActivatedRoute在这里因为你的 resolve()会得到 ActivatedRouteSnapshot和一个 RouterStateSnapshot .所以 resolve()看起来像这样:

  resolve(
route: ActivatedRouteSnapshot, state: RouterStateSnapshot
): Promise<any> {
return ...
}

..你可以使用route.params['id']获取您的 ID。


编辑:您还可以查看 Resolve 的文档

关于 Angular 2 : How to get params on resolve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41456663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com