错误:无法解析"rxjs/add/operator/map" [英] Error: Can't resolve 'rxjs/add/operator/map'

查看:184
本文介绍了错误:无法解析"rxjs/add/operator/map"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是app.module.ts,我试图在其他项目中完成地图的导入,并且工作正常,但是在此项目中却无法正常工作.

This is app.module.ts I have tried to done the Import of map in different project and it worked fine, but in this project it's not working.

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import {HttpModule} from '@angular/http';

    import { AppComponent } from './app.component';
    import { PagesComponent } from './pages/pages.component';

    @NgModule({
      declarations: [
        AppComponent,
        PagesComponent
      ],
      imports: [
        BrowserModule,
        HttpModule

      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

app.component.ts

import { Component } from '@angular/core';
import {PageService} from './page.service';

@Component({

  selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ["../assets/public/css/adminstyle.css",
            "../assets/public/css/demo.css",
          "../assets/public/css/style.css"
        ,"../assets/public/css/stylesheet.css"],
  providers:[PageService]
})
export class AppComponent {
  title = 'app';
}

page.service.ts

import {Injectable} from '@angular/core';
import {Http,Headers} from '@angular/http';

import 'rxjs/add/operator/map';

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

  constructor(private http:Http) { 
  console.log('Task Service Initialized');

  }
}

推荐答案

rxjs 6的使用有一些相当大的变化.

There are some pretty heavy change in the use of rxjs 6.

导入:

如上所述,您现在应该使用:

As already stated, you should now use :

import { map } from 'rxjs/operators';

(其他操作员也是如此)

(and same for other operators)

我想在这里提及其他主要变化:

I want to mention here the other main changes :

ObservableSubject和创建Observable s的方法(如of)现在必须这样导入:

Observable, Subject and methods that create Observables (like of) have now to be imported like that :

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

您将需要使用pipe来应用大多数运算符,这看起来可能有些奇怪.

You will need to use pipe to apply most operators, which might look a bit strange.

例如:

obs.pipe(
    map(....),
    secondOperator(....),
    thirdOperator()
)

代替

obs.map(....)
   .secondOperator(....)
   .thirdOperator()

最后,由于管道的变化以及与JavaScript保留字的冲突,一些操作符必须重命名:

And finally, due to the change with pipe and conflict with JavaScript reserved words, some operators had to be renamed :

do变为tap

catchfinally变为catchError finalize

switch变为switchAll

其他功能也被重命名:

fromPromise变为from

throw变为throwError

这篇关于错误:无法解析"rxjs/add/operator/map"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆