自从迁移到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:类型"Object"上不存在属性"subscribe"。
我的服务文件根本没有改变,布局如下
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");
}
}
花了一整天的时间在谷歌上搜索和处理代码,我不知道如何解决这个问题并使其编译,所以现在我希望有人知道如何用另一种方式来解决它,即我需要能够判断订阅何时发生并成功,但从我的组件内,最终结果应该会触发我的组件文件中的警报
将变量project
的类型更改为服务中的Observable<any>
或Observable<IProjectsMain[]>
。
export class ProjectsListComponent implements OnInit {
elementRef: ElementRef;
project: Observable<any>;
...
}
它以前工作过,因为您可能将tsconfig.json
更改为具有?:
"noEmitOnError": true