Angular RC 1 - 了解订阅何时成功
Angular RC 1 - Find out when a subscribe succeeds
自从迁移到 RC1 后,我 运行 遇到了一些问题,其中一个问题在我的一个组件中,我有一项服务可以从 laravel [=26] 中获取一些 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:属性 'subscribe' 在类型 'Object' 上不存在。
我的服务文件完全没有改变,布局如下
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
更改为 have?:
"noEmitOnError": true
自从迁移到 RC1 后,我 运行 遇到了一些问题,其中一个问题在我的一个组件中,我有一项服务可以从 laravel [=26] 中获取一些 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:属性 'subscribe' 在类型 'Object' 上不存在。
我的服务文件完全没有改变,布局如下
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
更改为 have?:
"noEmitOnError": true