我已将应用程序从4号角升级到6号角。我遇到了错误 在observable类型上不存在subscription。有人可以告诉我角度6是否有任何变化
import { Injectable } from '@angular/core';
import { Dto, ApiResult } from '../api';
import { RunsProxy } from '../api/proxies';
import { Observable, ReplaySubject, Subject } from 'rxjs';
import { AlertService } from './alert.service';
import { TranslateService } from '@ngx-translate/core';
import { ReadonlyProvider } from '@wtw/toolkit/src/directives/read-only-inputs.directive';
import { Router, NavigationStart, NavigationCancel, NavigationEnd } from '@angular/router';
import { CurrencyInfo, RunExecution } from '../api/dtos';
import { tap , map, share, delay } from 'rxjs/operators'
import { fireAndForget } from "platform/tests/helpers";
*public load(id: number): Observable<ApiResult<Dto.RunModel>> {
const obs = this._runs.get(id).uiSignal('load run').share();
obs.subscribe(ret => {
if (!!!ret.data && this.blnShown === false) {
this.blnShown = true;
this._translate.get('GLOBAL.TOASTS.RUN_UNAVAILABLE').subscribe(o => {
this._alertService.error(o);
});
}
this._activeRun.next(ret.data);
}, err => {
if (err.status === 403 || err.status === 404) {
this._router.navigate(['/home']);
this._alertService.clear();
this._translate.get('GLOBAL.TOASTS.RUN_UNAVAILABLE').subscribe(o => this._alertService.error(o));
} else throw err;
});
return obs;
}*
答案 0 :(得分:1)
这是我的服务示例。您可以根据需要输入相同的代码。
我的服务
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class PollSerivce {
constructor(private _http: Http) { }
getPollPostData(url: any) {
const options = new RequestOptions({ withCredentials: true });
return this._http.get(url)
.pipe(
catchError(this.handleError)
);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
我的组件文件
getDataSource() {
this._pollSubscription = this.pollSerivce.getPollPostData(StaticKeywords.baseUrl).subscribe(response => {
this.pollPostData = response.json().hits;
},
err => {
console.log(err);
});
}
rxjs 6有很多更改,因此,如果您想了解更多有关它的信息,可以访问以下网站::
https://auth0.com/blog/whats-new-in-rxjs-6/
https://www.academind.com/learn/javascript/rxjs-6-what-changed/