'rxjs/Observable' has no exported member 'Observable' in Angular

Dung Do Tien Nov 01 2021 156

Hello guys, I have create a service business file is StudentService, and I want to get a list of students.

I used Observable and of to help me return a list of students. Like this:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { Student } from './hero';
import { Students } from './mock-students';

@Injectable({
  providedIn: 'root'
})
export class StudentService {

  constructor() { 
    // Initial variable here
  }

  getStudent(): Observable<Student[]> {
    return of(Students);
  }

}

But when call method getStudent() I got an exception 'rxjs/Observable' has no exported member 'Observable'.

error TS2307: Cannot find module 'rxjs-compat/Observable'. node_modules/rxjs/observable/of.d.ts(1,15): 
error TS2307: Cannot find module 'rxjs-compat/observable/of'. src/app/student.service.ts(2,10): 
error TS2305: Module '"F:/learn-angular/node_modules/rxjs/Observable"' has no exported member 'Observable'. src/app/student.service.ts(15,12): 
error TS2304: Cannot find name 'of'.

How can I resolve it?

Have 2 answer(s) found.
  • B

    Behrad Nafar Nov 01 2021

    I think you only need to import the parent package. You can change:

    import { Observable } from 'rxjs/Observable';

    to

    import { Observable } from 'rxjs';

    In your service, you can do as below:

    import { Observable, of } from 'rxjs';

    I hope it works for you.

  • T

    Tran Quang Hung Nov 01 2021

    Sometimes I have to try to run npm update and npm install rxjs-compat --save and it works for me.

    If it still does not work you can try to delete node_modules folder first.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close