0

自从迁移到 RC1 后,我遇到了一些问题,其中一个在我的一个组件中,我有一个服务,它从 laravel api 获取一些 json 并订阅它,然后在组件中我有以下内容

        import {Component, ElementRef, OnInit} from '@angular/core';
        import {ProjectsMainApi} from "../../../services/projects-main";

        declare var jQuery: any;
        declare var subscribe: any;

        import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';

        @Component({
            selector: 'projects',
            templateUrl: './app/components/Projects/list/index.html',
            directives: [ROUTER_DIRECTIVES]
        })

        export class ProjectsListComponent implements OnInit {

            elementRef: ElementRef;

            project: Object;

            constructor(elementRef: ElementRef, private _projectsmainapi: ProjectsMainApi) {
                this.elementRef = elementRef;
                this.project = this._projectsmainapi.project$;

                this._projectsmainapi.getProjectsMain();        
            }
            ngOnInit() {


                this.project.subscribe(() => {

                   alert('subscribed');

                });
            }
        }

失败的部分

                this.project.subscribe(() => {

                   alert('subscribed');

                });

在升级编译器之前编译这个没有问题并且它工作但是自从升级我得到这个错误错误 TS2339: 属性'订阅'不存在于类型'对象'上。

我的服务文件根本没有改变,布局如下

    import {Http, Headers, Response} from "@angular/http"
    import {Injectable} from "@angular/core"
    import {IProjectsMain} from "../interfaces/IProjectsMain"
    import 'rxjs/Rx';
    import {Observable} from 'rxjs/Observable';
    import {Observer} from 'rxjs/Observer';
    import 'rxjs/add/operator/share';
    import 'rxjs/add/operator/map';

    @Injectable()
    export class ProjectsMainApi {

        apiUrl: string = "http://www.test.io/api/projects";

        headers: Headers = new Headers;

        project$: Observable<IProjectsMain[]>;

        private _ProjectsMainObserver: Observer<IProjectsMain[]>;

        private _dataStore: {

            project: IProjectsMain[]

        };

        constructor(private _http: Http) {

            this.headers.append('Content-Type', 'application/x-www-form-urlencoded');

            this.headers.append('X-Requested-With', 'XMLHttpRequest');

            //this.project$ = new Observable(observer => this._ProjectsMainObserver = observer).share();
            this.project$ = new Observable<IProjectsMain[]>(observer => this._ProjectsMainObserver = observer).share();
            this._dataStore = { project: [] };
        }

        public getProjectsMain() {

            this._http.get(this.apiUrl).map(response => response.json()).subscribe(data => {

                this._dataStore.project = data.project;

                this._ProjectsMainObserver.next(this._dataStore.project);

            }, error => console.log('Could not load projects.'),
                () => "doneskies");

        }

    }

花了一天的时间谷歌搜索和弄乱代码我不知道如何解决这个问题并让它编译所以现在我希望有人可能知道如何以另一种方式解决它,即我需要能够知道何时订阅发生并且它成功但在我的组件中,最终结果应该触发我的组件文件中的警报

4

1 回答 1

1

将变量的类型更改projectObservable<any>Observable<IProjectsMain[]>在您的服务中。

export class ProjectsListComponent implements OnInit {
    elementRef: ElementRef;
    project: Observable<any>;
...
}

它以前有效,因为您可能将您的更改tsconfig.json为 have?:

"noEmitOnError": true 
于 2016-05-05T22:23:47.190 回答