如何在 subscribe() 中访问这个 属性?

How to get access to this property inside subscribe()?

我有 属性 个内部组件:

public _flashMessagesService: FlashMessagesService;

以及以下代码:

return this.http.get(this.apiUrl.base_path + 'credentials')
      .map(res => res.json())
      .subscribe(
        credentials => {
          console.log(this._flashMessagesService);
          this._flashMessagesService.show('We are in about component!', { cssClass: 'alert-success', timeout: 1000 });
          this.saveToken(credentials);
        },
        err => {
          this._flashMessagesService.show('We are in about component!' + err, { cssClass: 'alert-error', timeout: 1000 });
          }
      );

我无法访问 this._flashMessagesService.show

在与作者讨论中发现的解决方案:

  1. _flashMessageService 没有在服务中工作(我们没有找到原因)。
  2. 它在组件内部正常工作。

我们所做的是将使用 _flashService 的登录移动到组件:

Service.ts:

@Injectable()
export class SomeService {
    constructor(private http: Http){}
    public login(user, pass): Observable<Response> {
        return this.http.get("someUrl");
    }
}

Component.ts:

@Component({
     //some component setup
})
export class MyComponent{
    constructor(private _flashMessagesService: FlashMessagesService, private service: SomeService)

    public method():void {
        this.service.login("aaa","bbb").map(res => res.json())
        .subscribe( credentials => {
            this._flashMessagesService.show('We are in about component!', { cssClass: 'alert-success', timeout: 1000 });
            this.saveToken(credentials);
        }, err => {
            this._flashMessagesService.show('We are in about component!' + err, { cssClass: 'alert-error', timeout: 1000 });
        });
    }
}