角度canActive()不返回值?

时间:2018-11-10 12:06:23

标签: angular

帮我检查下面的代码。怎么了?

this.authenticationService.isLogin().subscribe(s => {
            if (s == 10) {
                this.router.navigate(['/dashboard']);
                return false;
            }
        }, e => {
            // console.log(e);
            return true;
        })

1 个答案:

答案 0 :(得分:1)

如果您subscribe进入Observable,它将被消耗掉。您应该做的是使用map运算符来转换Observable包装的值。

在这里,尝试一下:

如果您使用的是Rxjs 5.5或更高版本:

import { map, catchError } from 'rxjs/operators';
import { of } from 'rxjs';

...
return this.authenticationService.isLogin().pipe(
  map(s => {
    if (s == 10) {
      this.router.navigate(['/dashboard']);
      return false;
    } else {
      // Not sure if this is what you want to return. 
      // But there needs to be an else condition as well for this.
      return true;
    }
  }),
  catchError(error => of(true))
)

如果您使用的是Rxjs 5或更早版本:

import 'rxjs/add/operator/map';
...
return this.authenticationService.isLogin()
  .map(s => {
    if (s == 10) {
      this.router.navigate(['/dashboard']);
      return false;
    }  else {
      // Not sure if this is what you want to return. 
      // But there needs to be an else condition as well for this.
      return true;
    }
  });