似乎无法在Observable上使用"first()"(Angular中的rxjs) [英] can't seem to use 'first()' on Observable(rxjs in Angular)

查看:118
本文介绍了似乎无法在Observable上使用"first()"(Angular中的rxjs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了RXJS中的这一出色功能,从理论上讲,它仅允许我订阅一个响应,但似乎无法实现.相反,我收到此错误,

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
    });
};

推荐答案

您需要

import'rxjs/add/operator/first';

在您的文件中.

这篇关于似乎无法在Observable上使用"first()"(Angular中的rxjs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆