Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

observable.of cannot be found in version 6.0.0-tactical-rc.1

Tags:

angular

rxjs

Im building service that should temporarily return Observable of some array before the server API will constructed. Before upgrading to angular and other packages to 6.0.0-rc.3 version i used following syntax:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';


@Injectable()
export class GeocodesService {
   getPositions():Observable<any>{     
        return Observable.of({ })
   }
}

At 6.0.0-rc.3, this syntax looks deprecated Trying to import it following way (using latest angular):

 import 'rxjs/observable/of'

But Getting error:

Module not found: Error: Can't resolve 'rxjs/observable/of'

How import of "of" can be done with latest rxjs?

Thanks

like image 596
happyZZR1400 Avatar asked Apr 11 '18 08:04

happyZZR1400


Video Answer


3 Answers

Finally found the solution here

This guy (Tomas Trajan), uses following syntax:

import { Observable , of} from 'rxjs'

and after you can use:

 getWidgets() {
    return of([]);
 }
like image 141
happyZZR1400 Avatar answered Oct 23 '22 09:10

happyZZR1400


If you want to use the patching imports with RxJS version 6, you will also need to install rxjs-compat.

It's a separate package that adds backwards compatibility (with RxJS version 5) to RxJS version 6:

npm install [email protected]

With rxjs-compat installed, your patching imports should work just fine.

For more information, see the migration guide.

like image 32
cartant Avatar answered Oct 23 '22 07:10

cartant


You must change your imports and your instantiation. Check out Damien's blog post

Tl;dr:

import { Observable, fromEvent, of } from 'rxjs';

const yourResult = Observable
    .create(of(yourObservable))
    .startWith(null)
    .map(x => x.someStringProperty.toLowerCase());

//subscribe to keyup event on input element
Observable
    .create(fromEvent(yourInputElement, 'keyup'))
    .debounceTime(5000)
    .distinctUntilChanged()
    .subscribe((event) => {
        yourEventHandler(event);
    });
like image 41
KVarmark Avatar answered Oct 23 '22 08:10

KVarmark