【问题标题】:Aurelia - Request web service in pipeline stepAurelia - 在管道步骤中请求 Web 服务
【发布时间】:2018-09-25 09:43:50
【问题描述】:

我正在尝试设置一个管道步骤“角色管理”,我需要在其中请求 Web 服务。现在我的问题是,http-request 是异步的,因此永远不会正确触发重定向。

 run(routingContext, next){
        if (routingContext.getAllInstructions().some(i => i.config.permission)) {

            let permission = routingContext.getAllInstructions()[0].config.permission;
            this.roleService.userIsAllowedTo(permission)
                .then(boolResponse => {
                    if(boolResponse){
                        return next();
                    }else{
                        return next.cancel(new Redirect("/"));
                    }
                });
        }
        return next();
    }

谁能告诉我如何解决这个问题?

【问题讨论】:

    标签: typescript httpclient aurelia


    【解决方案1】:

    只需从 run() 中返回 Promise

        return this.roleService.userIsAllowedTo(permission).then(boolResponse => {
                        if(boolResponse){
                            return next();
                        }else{
                            return next.cancel(new Redirect("/"));
                        }
                    });
    

    【讨论】:

      【解决方案2】:

      非常感谢。我现在可以通过将方法更改为异步方法来解决该问题:

      async run(routingContext, next){
              if (routingContext.getAllInstructions().some(i => i.config.permission)) {
      
                  let permission = routingContext.getAllInstructions()[0].config.permission;
                  let isallowed  = await this.roleService.userIsAllowedTo(permission);
                  if(isallowed){
                      return next();
                  }else{
                      return next.cancel();
                  }
              }
              return next();
          }
      

      【讨论】:

        猜你喜欢
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多