Failed to compile.
./src/app/hero.service.ts Module not found: Error: Can't resolve 'rxjs/Observable/of' in 'C:\Users\Admin\angular\myheroes\src\app'
@ ./src/app/hero.service.ts 13:11-40 @ ./src/app/app.module.ts @ ./src/main.ts @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts
my code for hero.service.ts
import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { of } from 'rxjs/Observable/of'; import { Hero } from './hero'; import { HEROES } from './mock-heroes'; import { MessageService } from './message.service'; @Injectable() export class HeroService { constructor(private messageService: MessageService) { } getHeroes(): Observable<Hero[]> { // todo: send the message _after_fetching the heroes this.messageService.add('HeroService: fetched heroes'); return of (HEROES); } }
To solve the error "Cannot find module 'rxjs-compat/Observable'", make sure to install the package by opening your terminal in your project's root directory and running the following command: npm i rxjs and restart your IDE and development server. Copied!
The of Operator is a creation Operator. Creation Operators are functions that create an Observable stream from a source. The of Operator will create an Observable that emits a variable amount of values in sequence, followed by a Completion notification.
I got this error because of an older version of rxjs and Angular 6 needs the latest version of rxjs.
This command installs the latest version that is compatible with Angular 6.
npm install --save rxjs-compat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With