Angular 2 - CanActivate未定义的值

时间:2016-08-30 14:49:55

标签: angular

我有一个问题 CanActivate()这是变量令牌没有收到函数的响应verifytoken总是返回undefined,我该怎么解决这个问题问题

代码

private verifytoken()
  {
        if(this._functionservice.getCookie("Cookie") != '' && this._functionservice.getCookie("Cookie") != "undefined")
    {
        this._symfonyservice.validtoken().subscribe(
            data => {this.resut = data;
                      if(this.resut['is_valid']==true)
                      {
                      console.log('true');

                        return true;
                     }
                    else
                    {
                        this._functionservice.deleteCookie("Cookie");
                        console.log('false');
                        this.router.navigate(['login']);
                        return false;
                    }
            },
            error =>{
                        alert("Sessão Expirada");
                        this._functionservice.deleteCookie("Cookie");
                        console.log('false');
                        this.router.navigate(['login']);
                        return false;
                    }
            );

    }
    else{
        this.router.navigate(['login']);
        return false;
    }

  }
  canActivate() {
    let token = this.verifytoken();
    console.log(token);
    return token;
  }

1 个答案:

答案 0 :(得分:0)

我在这里发现了两个问题:

private verifytoken()
  {
        if(this._functionservice.getCookie("Cookie") != '' && this._functionservice.getCookie("Cookie") != "undefined")
    {

     // 1 - You are returning nothing here, only inside the subscriptions. So,  you wont be able to get any data if you get inside this if statement
        this._symfonyservice.validtoken().subscribe(
            data => {this.resut = data;
                      if(this.resut['is_valid']==true)
                      {
                      console.log('true');

                        return true;
                     }
                    else
                    {
                        this._functionservice.deleteCookie("Cookie");
                        console.log('false');
                        this.router.navigate(['login']);
                        return false;
                    }
            },
            error =>{
                        alert("Sessão Expirada");
                        this._functionservice.deleteCookie("Cookie");
                        console.log('false');
                        this.router.navigate(['login']);
                        return false;
                    }
            );

    }
    else{
        // 2 - you are navigating to another place, so you wont be able to get any data from here
        this.router.navigate(['login']);
        return false;
    }

  }
  canActivate() {
    let token = this.verifytoken();
    console.log(token);
    return token;
  }

因此,您必须在if语句中返回订阅,否则您将无法获取任何数据。

---编辑--- 回答这个问题:

Promisses返回promisses,因此,您可以在调用方法后调用subscribe。

verifytoken()你必须返回promisse然后,在canActivate()你必须订阅这个promisse,就像我吼叫或同步等待价值:

private verifytoken(){
  if(this._functionservice.getCookie("Cookie") != '' && this._functionservice.getCookie("Cookie") != "undefined")
  {
    // here I returned the promisse
    return this._symfonyservice.validtoken().subscribe(
      data => {
        this.resut = data;

        if(this.resut['is_valid']==true){
            console.log('true');
            return true;
        }
        else
        {
            this._functionservice.deleteCookie("Cookie");
            console.log('false');
            this.router.navigate(['login']);
            return false;
        }
      },
      error =>{
          alert("Sessão Expirada");
          this._functionservice.deleteCookie("Cookie");
          console.log('false');
          this.router.navigate(['login']);
          return false;
      }
    );

  }
  else{
      this.router.navigate(['login']);
      return false;
  }

}

这里有一个技巧。你不能从promisse函数内部返回函数canActivate(),因为它的作用域。所以,你有两个选择:

1 - (异步)是调用promisse,你在那里调用canActivate,然后在那里调用whatch进行更改。

canActivate() {
  return this.verifytoken();
}

2 - (sync)是一段时间来检测何时触发promisse并将响应分配给token变量,这样你就可以返回它

canActivate() {
  var token = this.verifytoken();


  while(token == undefined){
    // wait until token has been set
  }

  return token;
}