加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2:如何在解决问题上获得参数

发布时间:2020-12-17 17:22:44 所属栏目:安全 来源:网络整理
导读:Routs策略: export const routes = [{ path : 'rto-activation/:id',component : RtoActivationComponent,resolve : { 'singleRto': SingleRtoResolve },children : [ {path: 'start',component: ActivationStartComponent},{path: 'warning',component: Wa
Routs策略:

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服务获得params.

问题:我可以从路由器服务获得参数.

简介:尝试在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应实现Resolve< T>.然后,您只需要在构造函数(){}中使用数据服务(RtoServicei guess).这里不需要路由器或ActivatedRoute,因为您的resolve()将获得ActivatedRouteSnapshot和RouterStateSnapshot.
所以resolve()会看起来像那样:

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

..你可以使用route.params [‘id’]来获取你的身份.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读