我在Angular中遇到HTTP问题 .
我只想 GET
JSON
列表并在视图中显示它 .
服务类
import {Injectable} from "angular2/core";
import {Hall} from "./hall";
import {Http} from "angular2/http";
@Injectable()
export class HallService {
public http:Http;
public static PATH:string = 'app/backend/'
constructor(http:Http) {
this.http=http;
}
getHalls() {
return this.http.get(HallService.PATH + 'hall.json').map((res:Response) => res.json());
}
}
在 HallListComponent
中,我从服务中调用 getHalls
方法:
export class HallListComponent implements OnInit {
public halls:Hall[];
public _selectedId:number;
constructor(private _router:Router,
private _routeParams:RouteParams,
private _service:HallService) {
this._selectedId = +_routeParams.get('id');
}
ngOnInit() {
this._service.getHalls().subscribe((halls:Hall[])=>{
this.halls=halls;
});
}
}
但是,我有一个例外:
TypeError:this.http.get(...) . map不是[null]中的函数
hall-center.component
import {Component} from "angular2/core";
import {RouterOutlet} from "angular2/router";
import {HallService} from "./hall.service";
import {RouteConfig} from "angular2/router";
import {HallListComponent} from "./hall-list.component";
import {HallDetailComponent} from "./hall-detail.component";
@Component({
template:`
<h2>my app</h2>
<router-outlet></router-outlet>
`,
directives: [RouterOutlet],
providers: [HallService]
})
@RouteConfig([
{path: '/', name: 'HallCenter', component:HallListComponent, useAsDefault:true},
{path: '/hall-list', name: 'HallList', component:HallListComponent}
])
export class HallCenterComponent{}
app.component
import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {HallCenterComponent} from "./hall/hall-center.component";
@Component({
selector: 'my-app',
template: `
<h1>Examenopdracht Factory</h1>
<a [routerLink]="['HallCenter']">Hall overview</a>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/hall-center/...', name:'HallCenter',component:HallCenterComponent,useAsDefault:true}
])
export class AppComponent { }
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
16 回答
我认为你需要导入这个:
或者更常见的是,如果您想要有更多可观察方法 . WARNING: This will import all 50+ operators and add them to your application, thus affecting your bundle size and load times.
有关详细信息,请参阅this issue .
全球进口是安全的 .
import'rxjs / Rx';
我有一个解决这个问题的方法
安装此包:
然后导入这个库
最后重启你的离子项目
从rxjs 5.5开始,您可以使用可管道运算符
What is wrong with the
import 'rxjs/add/operator/map';
当我们使用这种方法时,
map
运算符将被修补为observable.prototype
并成为此对象的一部分 .如果稍后,您决定从处理可观察流的代码中删除
map
运算符但未能删除相应的import语句,则实现map
的代码仍然是Observable.prototype
的一部分 .当捆绑器试图消除未使用的代码(a.k.a .
tree shaking
)时,即使它没有在应用程序中使用,它们也可能决定将map
运算符的代码保留在Observable中 .Solution -
Pipeable operators
可管理运算符是纯函数,不修补Observable . 您可以使用ES6导入语法
import { map } from "rxjs/operators"
导入运算符,然后将它们包装到一个函数pipe()
中,该函数采用可变数量的参数,即可链式运算符 .像这样的东西:
这是因为你正在使用rxjs并且在rxjs函数中不是静态的,这意味着你不能直接调用它们你必须调用管道内的方法并从rxjs库中导入该函数
但是如果你使用的是rxjs-compat,那么你只需要导入rxjs-compat运算符
对于Angular版本5及更高版本,更新的导入行如下所示:
要么
这些版本也完全支持Pipable Operators,因此您可以轻松使用.pipe()和.subscribe() .
如果您使用的是Angular版本2,那么以下行应该可以正常工作:
要么
如果您仍然遇到问题,那么您必须使用:
我不希望你直接使用它bcoz它会增加加载时间,因为它有大量的操作符(有用和无用的)根据行业规范不是一个好的做法,所以确保你首先尝试使用上面提到的导入行,如果这不起作用那么你应该去rxjs / Rx
由于 angular2 中的 Http 服务返回 Observable 类型,从您的Angular2安装目录(在我的情况下为'node_modules'),我们需要使用 http 服务在 component 中导入Observable的 Map 功能,如下所示:
Angular 6 - 只导入'rxjs / Rx'为我做了诀窍
Angular版本6“0.6.8”rxjs版本6“^ 6.0.0”
此解决方案适用于:
因为我们都知道角度每天都在发展,所以每天都有很多变化,这个解决方案适用于角度6和rxjs 6
首先使用http yo应该从以下位置导入:毕竟你必须在app.module.ts中声明HttpModule
你必须将HttpModule添加到Ngmodule - > imports
第二个使用map你应该先导入管道:
第三,你需要从以下 Map 导入 Map 功能:
你必须像这样的例子使用管道内的 Map :
这非常好运!
只需在您的文件中添加该行,
import'rxjs / Rx';
它将导入一堆依赖项 . 测试角度为5
你在这里使用的 map 不是javascript中的
.map()
,它是在Angular中Observables
上工作的Rxjs map函数...所以在这种情况下,如果你想在结果数据上使用map,你需要导入它...
所以只需像这样导入它:
将解决您的问题
我在角度5.2.0和rxjs 5.5.2中测试了它
使用Angular 5,RxJS导入得到了改进 .
代替
我们现在可以
没错,RxJs已将其 Map 运算符分离到一个单独的模块中,现在您需要像其他任何运算符一样明确地导入它 .
import rxjs/add/operator/map;
你会没事的
直接使用
Observable.subscribe
应该可行 .只是一些背景......新铸造的Server Communication开发指南(最后)讨论/提及/解释这个:
因此,@ Thierry已经回答,我们可以引入我们需要的运营商:
或者,如果我们懒惰,我们可以拉入全套操作员 . WARNING: this will add all 50+ operators to your app bundle, and will effect load times