【发布时间】:2017-05-18 07:43:09
【问题描述】:
溃败策略:
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) 打开任何子路由(隐式和显式)时,应在父路由中解析数据。
在任意子组件中激活路由时成功获取参数:
ngOnInit() {
let self = this;
this.subscription = this.route.params.subscribe(
(param: any) => {
let id = param[ 'id' ];
self.rtoService.getRto(null, +id)
.then((res: any) => {
})
});
}
【问题讨论】: