can't seem to use 'first()' on Observable(rxjs in Angular)

给你一囗甜甜゛ 提交于 2021-02-07 08:49:16

问题


I read about this awesome feature in RXJS which would theoretically allow me to subscribe only for one response, but can't seem to implement it. Instead, I'm getting this error,

"Property 'first' does not exist on type 'Observable'."

although I am just copy-pasting from existing examples. Here's my code:

implementHash( token ){
    console.log('trying to implement hash', token);
    const req = this.http.get('http://localhost:3003/token/' + token );
    const sub = req
        //.first()
        .subscribe( res =>{
            sub.unsubscribe; // trying to avoid this line
    });
};

回答1:


You need to

import 'rxjs/add/operator/first';

in your file.




回答2:


Add import 'rxjs/add/operator/first' to your component.ts file.

It comes from the import first.



来源:https://stackoverflow.com/questions/46328968/cant-seem-to-use-first-on-observablerxjs-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!