【发布时间】:2018-12-29 03:10:07
【问题描述】:
我需要帮助 :) 我正在开始创建授权服务,突然 http 客户端无法在此服务中工作,我不知道为什么,在玩了四个小时的代码后也没有任何想法。服务真的很简单。
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { UrlsService, Urls } from '@app/shared/urls';
import { Injectable } from '@angular/core'
@Injectable()
export class AuthorizationService {
private _permissions: string[];
constructor(
private httpClient: HttpClient,
private urlsService: UrlsService) {
}
refreshPermissions(): Observable<string[]> {
return this.httpClient
.get('http://localhost:54531/api/account/permissions', { observe: 'response'})
.map(response => {
console.log(response);
return this._permissions;
});
}
get permissions(): string[] {
return this._permissions;
}
refreshPermissions 方法调用每个用户的凭据更新。看看 app.component.ts
ngOnInit() {
this.authenticationService.setCredentialsSubject.subscribe(() => this.authorizationService.refreshPermissions());
this.authenticationService.loadCredentialsFromLocalStorage();
}
我已经在 map 函数上设置了断点来查看响应结构。但相反,我看到了错误。此外,在网络选项卡中,我可以看到该请求尚未触发。最后,map labmda 中的断点从未触发过。
我花了四个小时试图找出错误,但没有成功。你知道会发生什么吗?
附言。第一个屏幕的错误
"SyntaxError: Unexpected end of input
at AuthorizationService.refreshPermissions (webpack-internal:///./src/app/core/authentication/authorization/authorization.service.ts:14:14)
at SafeSubscriber.eval [as _next] (webpack-internal:///./src/app/app.component.ts:40:116)
at SafeSubscriber.__tryOrUnsub (webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:245:16)
at SafeSubscriber.next (webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:192:22)
at Subscriber._next (webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:133:26)
at Subscriber.next (webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:97:18)
at Subject.next (webpack-internal:///./node_modules/rxjs/_esm5/Subject.js:66:25)
at AuthenticationService.setCredentials (webpack-internal:///./src/app/core/authentication/authentication.service.ts:86:37)
at AuthenticationService.loadCredentialsFromLocalStorage (webpack-internal:///./src/app/core/authentication/authentication.service.ts:61:14)
at AppComponent.ngOnInit (webpack-internal:///./src/app/app.component.ts:41:36)"
【问题讨论】:
-
我不知道你的
httpClient是什么,但奇怪的是get()后面紧跟着map()。不应该有await,promise什么的吗? -
你也需要订阅 refreshPermissions:this.authenticationService.setCredentialsSubject.subscribe(() => this.authorizationService.refreshPermissions().subscribe(result => {}));
-
@DanielKhoroshko 我在代码示例中添加了导入部分,
httpClient来自@angular/common/http。httpClient.get返回且可观察。 -
@A.Winnen 谢谢,现在我明白了,如果没有订阅者 - observable 不会触发。
-
@A.Winnen 您可以发布答案,否则我稍后再做。
标签: angular typescript rxjs httpclient angular6