简体   繁体   English

错误 TS2304:找不到名称“Observable”Angularjs 2

[英]error TS2304: Cannot find name 'Observable' Angularjs 2

When I swithch to my Linux PC I get error like this:当我切换到我的 Linux PC 时,我收到如下错误:

 app/app.module.ts(21,67): error TS2304: Cannot find name 'Observable'. app/app.module.ts(25,53): error TS2304: Cannot find name 'Observable'. app/app.module.ts(29,68): error TS2304: Cannot find name 'Observable'. app/app.module.ts(33,67): error TS2304: Cannot find name 'Observable'. app/app.module.ts(37,56): error TS2304: Cannot find name 'Observable'. app/app.module.ts(52,27): error TS2304: Cannot find name 'Observable'. app/app.module.ts(52,50): error TS2304: Cannot find name 'Observable'. app/app.module.ts(54,40): error TS2304: Cannot find name '_'. app/app.module.ts(56,24): error TS2304: Cannot find name 'Observable'. app/app.module.ts(58,24): error TS2304: Cannot find name 'Observable'.

Anyone know solution for this?有人知道解决方案吗?

Also my editor show errors on HttpIntreceptor Class, when I remove it it works fine...另外我的编辑器在 HttpIntreceptor 类上显示错误,当我删除它时它工作正常......

This part is marked: Observable<Response>这部分标记为: Observable<Response>

Could you check it:你能检查一下吗:

class HttpInterceptor extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) {
        super(backend, defaultOptions);
    }

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.request(url, options));
    }

    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.get(url,options));
    }

    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {   
        return super.post(url, body);
    }

    put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.put(url, body, this.getRequestOptionArgs(options)));
    }

    delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.delete(url, options));
    }

    getRequestOptionArgs(options?: RequestOptionsArgs) : RequestOptionsArgs {
        if (options == null) {
            options = new RequestOptions();
        }
        if (options.headers == null) {
            options.headers = new Headers();
        }
        options.headers.append('Content-Type', 'application/json');
        return options;
    }

    intercept(observable: Observable<Response>): Observable<Response> {
        return observable.catch((err, source) => {
            if (err.status  == 401 && !_.endsWith(err.url, 'api/auth/login')) {

                return Observable.empty();
            } else {
                return Observable.throw(err);
            }
        });

    }
}

我通过导入 Observable 解决了同样的问题

import {Observable} from 'rxjs/Rx';

You can do it in 2 ways您可以通过 2 种方式进行

1. Import Observable and then import other functions like map, do, catch, throw whichever you are using 1.导入Observable,然后导入其他函数,比如map、do、catch、throw,随便你用哪个

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
.....
.....
import 'rxjs/add/observable/throw';

2. Importing whole Rxjs 2. 导入整个 Rxjs

import {Observable} from 'rxjs/Rx';

or

import {Observable} from 'rxjs';

Its recommended to use first method , since importing whole rxjs is not necessary and will include all the sub-modules into the bundle affecting bundle size and load time推荐使用第一种方法,因为没有必要导入整个 rxjs 并且会将所有子模块包含到包中,影响包大小和加载时间

没有一个解决方案有效,我不得不使用

import { Observable } from 'rxjs';

instead of using而不是使用

import 'rxjs/add/operator/map';
this.obObservable().map(data => {})

use使用

import { map } from "rxjs/operators";
this.obObservable().pipe(map(data => {}))

angular changes it recently角度最近改变了它

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

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