首页 文章

Angular2 beta.12和RxJs 5 beta.3的可观察错误

提问于
浏览
7

Hello,

我正在使用VS2015中运行的Angular2 beta 12 . 当我将rxjs从5.0.0-beta.2更新到beta.3时,我会遇到一系列与我的承诺有关的例外情况 .

例如 .

  • 属性 map 在类型 Observable<Response> 上不存在

  • 属性 share 在类型 Observable<Response> 中不存在

  • Ambient模块声明无法指定相对模块名称

  • Ambient模块不能嵌套在其他模块或名称空间中 .

的package.json

{
  "name": "ASP.NET",
  "version": "0.0.0",
  "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  },
  "dependencies": {
    "angular2": "2.0.0-beta.12",
    "systemjs": "0.19.24",
    "es6-promise": "3.1.2",
    "es6-shim": "0.35.0",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.3", // beta.2 allowed project to build
    "zone.js":"0.6.6"
  },
  "devDependencies": {
    "gulp": "3.9.1",
    "gulp-concat": "2.6.0",
    "gulp-cssmin": "0.1.7",
    "gulp-uglify": "1.5.3",
    "rimraf": "2.2.8",
    "concurrently": "2.0.0",
    "lite-server": "2.1.0",
    "typescript": "1.8.9"
  }
}

问题与此代码中的 Map 功能有关:

import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Headers, RequestOptions} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';

import {ApplicationVM} from '../../Applications/ViewModels/Application.ViewModel';

@Injectable()
export class ApplicationService {
    constructor(private http: Http) { }

    private _serviceUrl = './../api/';

    getApplications() {
        return this.http.get(this._serviceUrl + "applications/active")
            .map(res => <ApplicationVM[]>res.json())
           // .map((res: Response) => res.json())
            .do(data => console.log(data)) // eyeball results in the console
            .catch(this.handleError);
    }

    private handleError(error: Response) {
        console.log(error);
        return Observable.throw(error.json().error || 'Server error');
    }

}

在另一个,问题是 share()

constructor(private _http: Http) {
     console.log("constructor");
     this.menulist$ = new Observable(observer => this._menulistObserver = observer).share();
     this.menuState$ = new Observable(observer => this._menuStateObserver = observer).share();
     this.menuWidth$ = new Observable(observer => this._menuWidthObserver = observer).share();}

我觉得这可能很重要 - 一系列rxjs文件为相对引用加下划线红色 ../../Observable (以下示例在interval.d.ts中)

import { IntervalObservable } from '../../observable/IntervalObservable';
declare module '../../Observable' {
    namespace Observable {
        let interval: typeof IntervalObservable.create;
    }
}

我的boot.ts

///<reference path="./../node_modules/angular2/typings/browser.d.ts"/>
import {bootstrap}      from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS}    from 'angular2/http';
import 'rxjs/Rx'; // kitchen sink

// Bootstrap the application and reference the required directives
bootstrap(AppComponent, [ROUTER_PROVIDERS, HTTP_PROVIDERS]);

我的HTML页面

<!-- 1. Load libraries -->
    <script src="~/nodelibs/angular2/bundles/angular2-polyfills.js"></script>
    <script src="~/nodelibs/systemjs/system.src.js"></script>
    <script src="~/nodelibs/typescript/lib/typescript.js"></script>
    <script src="~/nodelibs/rxjs/bundles/Rx.js"></script>
    <script src="~/nodelibs/angular2/bundles/angular2.dev.js"></script>

    <script src="~/nodelibs/angular2/bundles/router.dev.js"></script>
    <script src="~/nodelibs/angular2/bundles/http.dev.js"></script>


    <!-- 2. Configure SystemJS -->
    <script>

    var rootPath = "@Url.Content("~/")";

    System.config({
        //transpiler: 'typescript',
        //typescriptOptions: { emitDecoratorMetadata: true },
        baseURL: rootPath,
        defaultJSExtensions: true,
        packages: {
            app: {
                //format: 'register',
                defaultExtension: 'js'
            }, map: {

                'rxjs/observable/*' : 'nodelibs/rxjs/observable/*.js',
                'rxjs/operators/*' : 'nodelibs/rxjs/operators/*.js',
                'rxjs/*' : 'nodelibs/rxjs/*.js'
            }
        }
    });
    System.import("/app/boot.js")
          .then(null, console.error.bind(console));

    </script>

我很难过,并会感激一些帮助 .

Thank, Dan.

3 回答

  • 15

    根据Angular的package.json,你应该使用RxJS 5.0.0-beta.2 https://github.com/angular/angular/blob/master/package.json#L37

  • 6

    关于运算符,您需要手动导入它们,因为它们默认情况下不包含在Observable类中 .

    为此你可以做到:

    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/share';
    

    或(包括所有运营商):

    import {Observable} from 'rxjs/Rx';
    

    否则,通常您不需要在 Map 块中将rxjs定义到SystemJS配置中 . 将相应的捆绑文件包含在脚本标记中就足够了 .

  • 5

    对于VS2015,GitHub上列出了此问题的解决方法here

    作为现在的解决方法,您可以使用https://raw.githubusercontent.com中的文件替换C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ CommonExtensions \ Microsoft \ TypeScript \ typescriptServices.js /Microsoft/TypeScript/Fix8518/lib/typescriptServices.js . 首先采取本地备份 .

    IMPORTANT :确保您使用的是VS2015更新 2 并安装了TypeScript> 1.8.2

    (在更新1上的文件后,我的VS无法启动)

    我使用的是rxjs 5.0.0-beta . 6 顺便说一句 .

相关问题