Module not found ..Cannot resolve module 'rxjs

2020-05-09 22:27发布

问题:

I am receiving an error when trying to build my application using angular 6 and have no idea what to do.

Module rxjs not found see this

Module not found ..Cannot resolve module 'rxjs/operators/toPromise  

what should i do ?

// the contact.service.ts

import { Injectable } from '@angular/core';
    import { Contact } from './contact';
    import { Http, Response } from '@angular/http';
    import 'rxjs/add/operator/toPromise';

    @Injectable()
    export class ContactService {
      private contactsUrl = '/api/contacts';
      constructor (private http: Http) {}
 .......
    }

回答1:

try to remove the import statement. It should work without that. Below code works.

new Observable().toPromise();

From rxjs 5.5, "toPromise: now exists as a permanent method on Observable"

https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md



回答2:

From rxjs 6 onwards toPromise is not an operator, it is part of observable object, you should be able to access directly without any imports