Observable.of is not a function

后端 未结 19 604
旧时难觅i
旧时难觅i 2020-11-30 19:41

I am having issue with importing Observable.of function in my project. My Intellij sees everything. In my code I have:

import {Observable} from          


        
相关标签:
19条回答
  • 2020-11-30 20:24

    For me (Angular 5 & RxJS 5) the autocomplete import suggested:

    import { Observable } from '../../../../../node_modules/rxjs/Observable';
    

    while to should be (with all static operators from, of, e.c.t working fine:

    import { Observable } from 'rxjs/Observable';
    
    0 讨论(0)
  • 2020-11-30 20:24
    import 'rxjs/add/observable/of';
    

    shows a requirement of rxjs-compat

    require("rxjs-compat/add/observable/of");
    

    I did not have this installed. Installed by

    npm install rxjs-compat --save-dev
    

    and rerunning fixed my issue.

    0 讨论(0)
  • 2020-11-30 20:25

    If anybody is having this problem while using Angular 6/rxjs 6 see the answers here: Could not use Observable.of in RxJs 6 and Angular 6

    In short, you need to import it like this:

    import { of } from 'rxjs';
    

    And then instead of calling

    Observable.of(res);
    

    just use

    of(res);
    
    0 讨论(0)
  • 2020-11-30 20:25

    You could also import all operators this way:

    import {Observable} from 'rxjs/Rx';
    
    0 讨论(0)
  • 2020-11-30 20:26

    If you are using Angular 6 /7

    import { of } from 'rxjs';
    

    And then instead of calling

    Observable.of(res);
    

    just use

    of(res);
    
    0 讨论(0)
  • 2020-11-30 20:30

    For Angular 5+ :

    import { Observable } from 'rxjs/Observable';should work. The observer package should match the import as well import { Observer } from 'rxjs/Observer'; if you're using observers that is

    import {<something>} from 'rxjs'; does a huge import so it's better to avoid it.

    0 讨论(0)
提交回复
热议问题