简体   繁体   English

Observable 类型上不存在属性管道

[英]Property pipe does not exist on type Observable

I am developing an auth guard in angular 8, but I'm getting the following error:我正在开发 angular 8 中的身份验证保护,但出现以下错误:

Property pipe does not exist on type Observable Observable 类型上不存在属性管道

This is happening when I call the isLoggedIn() function from the "canActivate" function which return Observable of AuthService clas.当我从返回 AuthService 类的 Observable 的“canActivate”函数调用isLoggedIn()函数时,就会发生这种情况。

Follows the AuthGuard service which implements the CanActivate interface:遵循实现CanActivate接口的AuthGuard服务:

import { Injectable } from "@angular/core";
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot
}  from "@angular/router";

import { AuthService } from "../login/auth.service";
import { map, take } from "rxjs/operators";
import { merge } from "rxjs";
import { Observable } from "rxjs";

@Injectable({ providedIn: "root" })
export class AuthGuard implements CanActivate {
constructor(
private router: Router,
private authenticationService: AuthService
) {}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> {
return this.authenticationService.isLoggedIn().pipe(
  take(1),
  map(isLoggedIn => {
    if (isLoggedIn) {
      return true;
    } else {
      return false;
    }
   })
 );
 }
}

Here is my AuthService :这是我的AuthService

 import { Injectable } from "@angular/core";
 import { HttpClient } from "@angular/common/http";
 import { Observable, BehaviorSubject } from "rxjs";
 import { map } from "rxjs/operators";

 @Injectable({
 providedIn: "root"
 })
export class AuthService {
url = "http://localhost:8098/login";

isLogged: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

 constructor(private http: HttpClient) {
  this.isLogged = new BehaviorSubject<boolean>(this.tokenAvailable());
 }

 isLoggedIn() {
   return this.isLogged.asObservable;
 }

 public login(credential): Observable<boolean> {
 return this.http.post(this.url, credential).pipe(
  map(data => {
    localStorage.setItem("token", data["token"]);
    this.isLogged.next(true);
    return true;
  })
  );
 }

 public logout() {
   localStorage.removeItem("token");
   this.isLogged.next(false);
 }

  getToken(): string {
    let token = localStorage.getItem("token");
    return token;
  }

 tokenAvailable(): boolean {
   let token = this.getToken();
   return !token ? false : true;
  }
 }

And here is my package.json这是我的 package.json

            {
          "name": "angular-auth",
          "version": "0.0.0",
          "scripts": {
            "ng": "ng",
            "start": "ng serve",
            "build": "ng build",
            "test": "ng test",
            "lint": "ng lint",
            "e2e": "ng e2e"
          },
          "private": true,
          "dependencies": {
            "@angular/animations": "~8.2.14",
            "@angular/common": "~8.2.14",
            "@angular/compiler": "~8.2.14",
            "@angular/core": "~8.2.14",
            "@angular/forms": "~8.2.14",
            "@angular/platform-browser": "~8.2.14",
            "@angular/platform-browser-dynamic": "~8.2.14",
            "@angular/router": "~8.2.14",
            "jwt-decode": "^2.2.0",
            "rxjs": "^6.4.0",
            "tslib": "^1.10.0",
            "zone.js": "~0.9.1"
          },
          "devDependencies": {
            "@angular-devkit/build-angular": "~0.803.21",
            "@angular/cli": "~8.3.21",
            "@angular/compiler-cli": "~8.2.14",
            "@angular/language-service": "~8.2.14",
            "@types/node": "~8.9.4",
            "@types/jasmine": "~3.3.8",
            "@types/jasminewd2": "~2.0.3",
            "codelyzer": "^5.0.0",
            "jasmine-core": "~3.4.0",
            "jasmine-spec-reporter": "~4.2.1",
            "karma": "~4.1.0",
            "karma-chrome-launcher": "~2.2.0",
            "karma-coverage-istanbul-reporter": "~2.0.1",
            "karma-jasmine": "~2.0.1",
            "karma-jasmine-html-reporter": "^1.4.0",
            "protractor": "~5.4.0",
            "ts-node": "~7.0.0",
            "tslint": "~5.15.0",
            "typescript": "~3.5.3"
          }
        }

The asObserbalbe is a function and not a property, as can be seen in here . asObserbalbe是一个函数而不是一个属性,可以在这里看到。

So you need to call it in your AuthService like this:所以你需要像这样在你的AuthService调用它:

export class AuthService {

     ......

     isLoggedIn() {
        return this.isLogged.asObservable();
     }

     ......

}

If you want to know more about BehaviorSubject you can check this and more about rxjs pipes in here .如果您想了解有关BehaviorSubject更多信息,您可以在此处查看此内容以及有关rxjs pipes更多信息。

asObservable()是一个函数,需要用()调用

asObservable是一个函数,所以你的isLoggedIn方法应该返回this.isLogged.asObservable()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;Observable&#39; 类型不存在属性 &#39;catch&#39;<any> &#39; - Property 'catch' does not exist on type 'Observable<any>' “Observable”类型上不存在属性“过滤器”<Event> &#39; - Property 'filter' does not exist on type 'Observable<Event>' Observable 类型上不存在属性“then” <querysnapshot<documentdata> &gt; </querysnapshot<documentdata> - Property 'then' does not exist on type Observable<QuerySnapshot<DocumentData>> 'void'类型上不存在属性'管道' Angular 9 - Property 'pipe' does not exist on type 'void' Angular 9 属性&#39;mergeMap&#39;在类型&#39;Observable上不存在 <any> “ - Property 'mergeMap' does not exist on type 'Observable<any>' Angular 6:“Observable”类型上不存在“map”属性<Response> &#39; - Angular 6: Property 'map' does not exist on type 'Observable<Response>' “未知”可观察类型上不存在属性“过滤器”<course[]> 在 Rxjs 6</course[]> - Property 'filter' does not exist on type 'unknown' Observable<Course[]> in Rxjs 6 错误:类型&#39;OperatorFunction &lt;{},{} |属性&#39;subscribe&#39;不存在 可观察的 <any> &gt; - Error: property 'subscribe' does not exist on type 'OperatorFunction<{}, {} | Observable<any>> Angular 6:“typeof Observable”类型上不存在“fromEvent”属性 - Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable' 类型 'void | 上不存在属性 'subscribe' 可观察的<user> '。 ts(2339)</user> - Property 'subscribe' does not exist on type 'void | Observable<User>'. ts(2339)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM