简体   繁体   中英

Argument of type '1' is not assignable to parameter of type 'number[]'

I am using Angular4 and currently started exploring Services of Angular4. I have done everything right in my dataServices class but still i am getting this error in argument of observer.next(1); \\

Please tell me where i am wrong.

Error:

Argument of type '1' is not assignable to parameter of type 'number[]'

data.service.ts:

 import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; @Injectable() export class DataService{ data: Observable<Array<number>>; constructor(){ } getData(){ this.data = new Observable(observer =>{ setTimeout(() =>{ observer.next(1); }, 1000); setTimeout(() =>{ observer.next(2); }, 2000); setTimeout(() =>{ observer.next(3); }, 3000); setTimeout(() =>{ observer.next('Hello'); }, 4000); setTimeout(() =>{ observer.complete(); }, 5000); }); return this.data; } } 

this.data is an Array of numbers, not a single number. Push your values to this.data instead.

You wrote this type for data :

data: Observable<Array<number>>;

But you're trying to pass a number , not an array of numbers. So you can just change the type of data :

data: Observable<number>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM