[英]Angular: error TS2322: Type 'Observable<{}>' is not assignable … with share() operator
I'm new to Angular 5 and I'm implementing the Observable/Observer pattern in order to share some events and some data changes to the subcribers. 我是Angular 5的新手,我正在实现Observable / Observer模式,以便向用户共享一些事件和一些数据更改。
Here the snippet of code: 这是代码段:
...
@Injectable()
export class NidoService {
...
event$: Observable<StrutturaDto[]>;
private _observer: Observer<any>;
event_dettaglio$: Observable<Struttura>;
private _observer_dettaglio: Observer<any>;
constructor() {
this.event$ = new Observable(observer => this._observer = observer).share();
this.event_dettaglio$ = new Observable(observer => this._observer_dettaglio = observer).share();
}
...
}
When I try to build the application with ng build I get these errors on the two lines of code within the constructor : 当我尝试使用ng build来构建应用程序时,在构造函数中的两行代码上出现了这些错误:
ERROR in src/app/services/nido.service.ts(28,7): error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable<StrutturaDto[]>'.
Type '{}' is not assignable to type 'StrutturaDto[]'.
Property 'includes' is missing in type '{}'.
src/app/services/nido.service.ts(29,7): error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable<Struttura>'.
Type '{}' is not assignable to type 'Struttura'.
Property 'idStruttura' is missing in type '{}'.
When I use it in development mode I have no problems, but I cannot build without fixing them. 当我在开发模式下使用它时,我没有任何问题,但是如果不修复它们就无法构建。 How can I change the two lines of code?
如何更改两行代码?
You need to specify the type of observable: 您需要指定可观察的类型:
this.event$ = new Observable<StrutturaDto[]>(observer => this._observer = observer).share();
and 和
this.event_dettaglio$ = new Observable<Struttura>(observer => this._observer_dettaglio = observer).share();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.