错误:无法解析“rxjs/add/operator/map" [英] Error: Can't resolve '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';
(其他运营商也一样)
我想在这里提到其他主要变化:
I want to mention here the other main changes :
Observable
、Subject
和创建 Observable
的方法(如 of
)现在必须像这样导入那:
Observable
, Subject
and methods that create Observable
s (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
catch
和 finally
变成 catchError
finalize
switch
变成 switchAll
其他函数也被重命名:
fromPromise
变成 from
throw
变成 throwError
这篇关于错误:无法解析“rxjs/add/operator/map"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!