将变量类型更改为 Observable 后的错误 (Angular 7)
Errors after changing the type of a variable to an Observable (Angular 7)
将变量的类型更改为 Observable 后,我在代码的其他地方收到无法处理的错误消息。
authService 中的 Observable 我是这样创建的:
private activeBusinessCase = new BehaviorSubject<string>(null);
public activeBusinessCase$ = this.activeBusinessCase.asObservable();
在同一个服务中,我收到错误信息..
Property 'toLowerCase' does not exist on type 'BehaviorSubject '
..对于这行代码:
this.router.navigate ([Constants.routing.explorer + this.activeBusinessCase.toLowerCase ()]);
在我的 home.component.ts 中,我收到以下错误消息..
Property 'activeBusinessCase' is private and only accessible within class 'UserAuthService'.
..对于这行代码:
ngOnInit () {
this.router.navigate ([Constants.routing.explorer + this.authService.activeBusinessCase.toLowerCase ()]);
}
但最重要的错误消息在我的 httpService 中,其中不再起作用:
getResource (key: string, lang: string): Observable <any> {
const headers = new HttpHeaders ({'Accept': 'text / html'});
return this.httpClient.get ('/ resources /' + key,
{
headers: headers,
responseType: 'text',
params: new HttpParams ()
.set ('businessCase', this.authService.activeBusinessCase? this.authService.activeBusinessCase:
environment.default_business_case)
.set ('long', long)
}) Pipe. (
catchError (() => {
return this.translateService.get ('Not-available'). pipe (
map (res => '<h4 style = "text-align: center">' + res + '</ h4>'));
})
);
}
getResources (long: string): Observable <Resource []> {
return this.httpClient.get <any> ('/ resources',
{
params: new HttpParams ()
.set ('long', long)
.set ('businessCase', this.authService.activeBusinessCase? this.authService.activeBusinessCase:
environment.default_business_case)
});
}
控制台中的错误信息如下:
error TS2341: Property 'activeBusinessCase' is private and only accessible within class 'UserAuthService'.
我仍然是 Angular 的初学者,感谢任何详细的帮助。
我可以试着帮忙,但你没有问题,反正...
1.you 不能对除字符串以外的任何其他内容使用 toLowerCase(observable/subject 可以发出字符串但它不是字符串)
observables
- 如果您将 属性 设置为私有,您将无法在其他任何地方使用它。使用 public 一个。
您到底想实现什么目标?
将变量的类型更改为 Observable 后,我在代码的其他地方收到无法处理的错误消息。
authService 中的 Observable 我是这样创建的:
private activeBusinessCase = new BehaviorSubject<string>(null);
public activeBusinessCase$ = this.activeBusinessCase.asObservable();
在同一个服务中,我收到错误信息..
Property 'toLowerCase' does not exist on type 'BehaviorSubject '
..对于这行代码:
this.router.navigate ([Constants.routing.explorer + this.activeBusinessCase.toLowerCase ()]);
在我的 home.component.ts 中,我收到以下错误消息..
Property 'activeBusinessCase' is private and only accessible within class 'UserAuthService'.
..对于这行代码:
ngOnInit () {
this.router.navigate ([Constants.routing.explorer + this.authService.activeBusinessCase.toLowerCase ()]);
}
但最重要的错误消息在我的 httpService 中,其中不再起作用:
getResource (key: string, lang: string): Observable <any> {
const headers = new HttpHeaders ({'Accept': 'text / html'});
return this.httpClient.get ('/ resources /' + key,
{
headers: headers,
responseType: 'text',
params: new HttpParams ()
.set ('businessCase', this.authService.activeBusinessCase? this.authService.activeBusinessCase:
environment.default_business_case)
.set ('long', long)
}) Pipe. (
catchError (() => {
return this.translateService.get ('Not-available'). pipe (
map (res => '<h4 style = "text-align: center">' + res + '</ h4>'));
})
);
}
getResources (long: string): Observable <Resource []> {
return this.httpClient.get <any> ('/ resources',
{
params: new HttpParams ()
.set ('long', long)
.set ('businessCase', this.authService.activeBusinessCase? this.authService.activeBusinessCase:
environment.default_business_case)
});
}
控制台中的错误信息如下:
error TS2341: Property 'activeBusinessCase' is private and only accessible within class 'UserAuthService'.
我仍然是 Angular 的初学者,感谢任何详细的帮助。
我可以试着帮忙,但你没有问题,反正...
1.you 不能对除字符串以外的任何其他内容使用 toLowerCase(observable/subject 可以发出字符串但它不是字符串) observables
- 如果您将 属性 设置为私有,您将无法在其他任何地方使用它。使用 public 一个。
您到底想实现什么目标?