我正在尝试使用http get从api中检索布尔值,并遇到Angular 6的问题(代码在v5中运行):
我的userservice.ts:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs/Observable';
@Injectable({
providedIn: 'root',
})
export class UserService {
readonly baseUrl = `${environment.apiUrl}/user`;
constructor(private http: HttpClient) { }
public isAdmin(): Observable<boolean> {
return this.http.get<boolean>(this.baseUrl + '/isAdmin');
}
}
我的组件:
export class TestComponent{
public admin: boolean;
constructor(private userService: UserService) {
this.userService.isAdmin()
.subscribe(
result => {
this.admin = result;
}
);
}
}
当我导航到我的组件时,我在控制台中遇到的错误是this.userService.isAdmin不是函数