Best way to import Observable from rxjs

前端 未结 3 730
醉梦人生
醉梦人生 2020-11-28 20:09

In my angular 2 app I have a service that uses the Observable class from the rxjs library.

import { Observable } from \'rxjs\';
         


        
3条回答
  •  没有蜡笔的小新
    2020-11-28 20:39

    One thing I've learnt the hard way is being consistent

    Watch out for mixing:

     import { BehaviorSubject } from "rxjs";
    

    with

     import { BehaviorSubject } from "rxjs/BehaviorSubject";
    

    This will probably work just fine UNTIL you try to pass the object to another class (where you did it the other way) and then this can fail

     (myBehaviorSubject instanceof Observable)
    

    It fails because the prototype chain will be different and it will be false.

    I can't pretend to understand exactly what is happening but sometimes I run into this and need to change to the longer format.

提交回复
热议问题