我得到了这个错误,但我不知道为什么。提前谢谢。Picture of the error
我认为错误发生在我的reddit.service.ts文件夹中
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class RedditService{
http: any;
baseUrl: String;
constructor(http:Http){
this.http = http;
this.baseUrl = 'https://www.reddit.com/r';
}
getPosts(category, limit){
return this.http.get(this.baseUrl+'/'+category+'/top.json?limit='+limit)
.map(res => res.json());
}
}发布于 2018-01-18 23:50:16
尝试使用以下代码:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class RedditService{
http: any;
baseUrl: String;
constructor(public http:Http){
this.http = http;
this.baseUrl = 'https://www.reddit.com/r';
this.getPosts();
}
getPosts(category, limit){
return this.http.get(this.baseUrl+'/'+category+'/top.json?limit='+limit).map(res => res.json()).subscribe(data => {
console.log(data);
});
}
}注意:在构造函数方法中,如果没有添加private或public关键字,则service/Http变量将作为局部变量进行计算,因此在方法调用结束时会销毁该变量。
https://stackoverflow.com/questions/48323612
复制相似问题