2

我必须在我的测试和这个小代码中使用 Observable:

it("should emit values", () => {
   const f = Observable.of([1, 3, 5]);
   f.subscribe(d => console.log(d));
});

给出以下错误:

TypeError: undefined is not a constructor (评估 'Observable_1.Observable.of([1, 3, 5])')

但是,如果我将这两行代码放在一个组件中(例如,在 ngOnInit 中),我会看到发出的值并且没有错误。我想知道这个 me 或 angular 2 的测试代码库是否有问题?

以防万一,这些都是我在这个特定测试文件中使用的所有导入:


import {
it,
inject,
injectAsync,
beforeEachProviders,
TestComponentBuilder
} from 'angular2/testing';

import {ComponentFixture, dispatchEvent, fakeAsync, tick} from 'angular2/testing_internal';

import {provide, bind} from 'angular2/core';
import {FormBuilder} from 'angular2/common';
import {By} from 'angular2/platform/browser';
import {Observable} from 'rxjs/Observable';
import {ArrayObservable} from 'rxjs/observable/fromArray';
import {Subject} from 'rxjs/Subject';
import {BehaviorSubject} from 'rxjs/subject/BehaviorSubject';

import {SpyLocation} from 'angular2/src/mock/location_mock';
import {RootRouter} from 'angular2/src/router/router';
import {Router, RouterOutlet, RouterLink, RouteParams, ComponentInstruction, RouteData, Location, ROUTER_PRIMARY_COMPONENT} from 'angular2/router';
import {
RouteConfig,
Route,
AuxRoute,
AsyncRoute,
Redirect
} from 'angular2/src/router/route_config_decorator';
import {RouteRegistry} from 'angular2/src/router/route_registry';
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
import {ResolvedInstruction} from 'angular2/src/router/instruction';

4

1 回答 1

1

您是否尝试过更改import {Observable} from 'rxjs/Observable';import {Observable} from 'rxjs/Rx';? 这可能会有所帮助,因为以这种方式导入它将使您获得您可能在组件中使用的所有操作。

于 2016-04-15T07:02:44.853 回答