简体   繁体   中英

Observable.combineLatest is not a function

I have a Home page, somewhere in it the user clicks on Contact me to be redirected to the Contact page:

home.component.html

<div>
  <a routerLink="/contact" [queryParams]="sendOBj">Contact me</a>
</div>

home.component.ts:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '../../../node_modules/@angular/router';
import { FollowersService } from '../followers.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  myfollowers: any[]; 
  sendOBj: {id: any, name: any};

  constructor(private followers: FollowersService, private route: ActivatedRoute) { }

  ngOnInit() {
    this.myfollowers = this.followers.getFollowers();   
    this.sendOBj = {id: this.myfollowers[0].id, name: this.myfollowers[0].name };
  }

}

contact.component.ts:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '../../../node_modules/@angular/router';
import { Observable } from '../../../node_modules/rxjs/Observable';
import 'rxjs/observable/combineLatest';


@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.css']
})
export class ContactComponent implements OnInit {

  constructor( private route: ActivatedRoute) { }

  ngOnInit() {

    Observable.combineLatest([
      this.route.queryParamMap
    ])
      .subscribe(
        combined=>{
          let id = combined[1].get('id');
          console.log('id', id);
        }
      );

    this.route.queryParamMap.subscribe();    
  }
}

By clicking on Contact me on Home , I get this error:

ContactComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Observable_1.Observable.combineLatest is not a function

Please help me find out the problem.

In angular 6,

you just need:

import {combineLatest} from "rxjs/index";

and replace

Observable.combineLatest(...)

by

combineLastest(...)

I solved the problem this way:

I first upgraded to Angular 6, then modified contact.component.ts like this:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '../../../node_modules/@angular/router';    
import { combineLatest } from 'rxjs/operators';
import { CombineLatestSubscriber } from '../../../node_modules/rxjs/operators/combineLatest';



@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.css']
})
export class ContactComponent implements OnInit {

  constructor( private route: ActivatedRoute) { }

  ngOnInit() {

      this.route.paramMap.subscribe();

      this.route.queryParamMap.subscribe(
        user=>{
          console.log('username: ', user.get('name'));
          console.log('user id', user.get('id'));
        }
      );        

  }
}

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