Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular canActive()不返回值?_Angular - Fatal编程技术网

Angular canActive()不返回值?

Angular canActive()不返回值?,angular,Angular,帮我检查下面的代码。怎么了 this.authenticationService.isLogin().subscribe(s => { if (s == 10) { this.router.navigate(['/dashboard']); return false; } }, e => { // console.log(e);

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

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

如果您订阅可观察对象,它将被消费。您应该做的是使用
map
操作符转换由可观察对象包装的值

在这里,尝试一下:

如果您使用的是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;
    }
  });

美好的我会努力的!此代码是否属于
canActive()
route gaurd?