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

时间:2016-10-11 17:20:27

标签: angular typescript

当我切换到我的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'.

任何人都知道解决方案吗?

此外,我的编辑器在HttpIntreceptor类上显示错误,当我删除它时它工作正常......

此部分标记为:Observable<Response>

你可以检查一下:

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

    }
}

4 个答案:

答案 0 :(得分:37)

我通过导入Observable

修复了同样的问题
import {Observable} from 'rxjs/Rx';

答案 1 :(得分:13)

你可以通过两种方式完成

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。导入整个Rxjs

import {Observable} from 'rxjs/Rx';

import {Observable} from 'rxjs';

建议使用第一种方法,因为不需要导入整个rxjs,并且会将所有子模块包含到捆绑中,从而影响捆绑包大小和加载时间

答案 2 :(得分:1)

代替使用

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

使用

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

角度最近更改了

答案 3 :(得分:1)

所有解决方案都无法使用,我不得不使用

import { Observable } from 'rxjs';