简体   繁体   English

将变量类型更改为Observable后出现错误(角度7)

[英]Errors after changing the type of a variable to an Observable (Angular 7)

After changing the type of a variable to an Observable, I get error messages elsewhere in my code that I can not handle. 将变量的类型更改为Observable后,我在代码中的其他地方收到了无法处理的错误消息。

The Observable within a authService I have created like this: 我在authService中创建的Observable如下所示:

private activeBusinessCase = new BehaviorSubject<string>(null);
public activeBusinessCase$ = this.activeBusinessCase.asObservable();

In the same service, I receive the error message.. 在同一服务中,我收到错误消息。

Property 'toLowerCase' does not exist on type 'BehaviorSubject ' 类型“ BehaviorSubject”上不存在属性“ toLowerCase”

..for this line of code: ..对于这一行代码:

this.router.navigate ([Constants.routing.explorer + this.activeBusinessCase.toLowerCase ()]);

Within my home.component.ts I get the following error message.. 在我的home.component.ts中,我收到以下错误消息。

Property 'activeBusinessCase' is private and only accessible within class 'UserAuthService'. 属性“ activeBusinessCase”是私有的,只能在“ UserAuthService”类中访问。

..for this lines of code: ..对于这行代码:

ngOnInit () {
  this.router.navigate ([Constants.routing.explorer + this.authService.activeBusinessCase.toLowerCase ()]);
}

But the most important error message is in my httpService , in which nothing works anymore: 但是最重​​要的错误消息是在我的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)
      });
  }

Here the error message in the console sais: 控制台中的错误消息如下:

error TS2341: Property 'activeBusinessCase' is private and only accessible within class 'UserAuthService'. 错误TS2341:属性'activeBusinessCase'是私有的,只能在类'UserAuthService'中访问。


I am still a beginner in Angular and thankful for any detailed help. 我仍然是Angular的初学者,感谢您提供的详细帮助。

I can try to help but you dont have a question, anyway... 我可以尝试提供帮助,但是无论如何您都没有问题...

1.you cant use toLowerCase on anything else than a string ( the observable/subject can emit a string but its not a string) observables 在别的不是字符串1.you不能使用toLowerCase(可观察/主题可以发出一个字符串,但它不是一个字符串) 可观测量

  1. if you set a property private you cant use it anywhere else. 如果将属性设为私有,则不能在其他任何地方使用它。 use the public one. 使用公共的。

what do you exactly want to archieve? 您到底想归档什么?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM