问题描述
这是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 :
Observable
,Subject
和创建Observable
s的方法(如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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!