angular - Can't get basic Observables to work -


i've been learning angular, , far i've got basic foundations down. want dive deeper rxjs , observables every time try follow tutorials online, there these trivials errors, feel stupid can't solve them, , stuck on first step of these tutorials. example, i'm following rangle.io's tutorial on observables, .next(42) .next(43), , .push(value) returns error saying argument of type '42' not assignable paramter of type 'number[]'.

in tutorials , rxjs official github, use import rx 'rxjs/rx' or import * rx 'rxjs'rx, doesn't work in project. can't figure out why have use import { observable } 'rxjs/observable' instead of importing everything.

anyway, missing causing error in operator?

import { component } '@angular/core';  import { observable } 'rxjs/observable'; import 'rxjs/observable/from';  @component({   selector: 'app-root',   templateurl: './app.component.html',   styleurls: ['./app.component.css'] }) export class appcomponent {   title = 'app';    private data: observable<array<number>>;   private values: array<number> = [];   private anyerrors: boolean;   private finished: boolean;    constructor() {}    init() {     this.data = new observable(observer => {       settimeout(() => {         observer.next(42);       }, 1000);        settimeout(() => {         observer.next(43);       }, 1000);        settimeout(() => {         observer.complete();       }, 3000);     });      const subscription = this.data.subscribe(       value => this.values.push(value),       error => this.anyerrors = true,       () => this.finished = true     );   } } 

the error you're seeing because you're declaring observable produce elements of type array<number> you're setting produce number values.

if change

private data: observable<array<number>>;

to

private data: observable<number>;

that should resolve issue.

remember observables should set type of each object emit e.g. <number>, rather list or collection of emissions <array<number>> *

*unless of course observable emit collection of objects every time...


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -