简体   繁体   English

角度:错误TS2322:类型'Observable <{}>'不可分配...使用share()运算符

[英]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.

相关问题 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 角度错误 TS2322:&#39;Promise<Dish | undefined> &#39; 不可分配给类型 &#39;Promise<Dish> &#39; - Angular error TS2322:'Promise<Dish | undefined>' is not assignable to type 'Promise<Dish>' angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string typescript(错误 TS2322):类型'{类型:ErrorPageType; }' 不可分配给类型 'ErrorPageType.SIGNUP' - typescript (error TS2322) : Type '{ type: ErrorPageType; }' is not assignable to type 'ErrorPageType.SIGNUP' “Firebase”类型不能分配给“null”类型。 TS2322 - Type 'Firebase' is not assignable to type 'null'. TS2322 TS2322:类型“计数”不可分配给类型“ReactNode” - TS2322: Type 'Count' is not assignable to type 'ReactNode' TS2322:类型“BillingSummaryDTO[]”不可分配给类型“从不” - TS2322: Type 'BillingSummaryDTO[]' is not assignable to type 'never' 类型“IntersectionObserver”不可分配给类型“null”。 TS2322 [React,intersectionObserver] - Type 'IntersectionObserver' is not assignable to type 'null'. TS2322 [React, intersectionObserver] TypeScript TS2322:类型'typeof Foo'不能分配给'IFoo'类型 - TypeScript TS2322: Type 'typeof Foo' is not assignable to type 'IFoo' Typescript - React 错误 TS2322:类型 'AppState | undefined' 不能分配给类型 'ICards | 不明确的' - Typescript - React Error TS2322: Type 'AppState | undefined' is not assignable to type 'ICards | undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM