[英]Why angular Service data is get undefined if I call from component
I am Using JSONPlace holder Fake API for fetching data.我正在使用 JSONPlace holder Fake API 来获取数据。 I am getting the api data in service but if I call that service from my app component I am getting
undefined
why this is my code我正在获取服务中的 api 数据,但是如果我从我的应用程序组件调用该服务,我将
undefined
为什么这是我的代码
My Service.ts is我的 Service.ts 是
import { Injectable } from '@angular/core';
import { Http, URLSearchParams, Response, RequestOptions, Headers } from '@angular/http';
import 'rxjs/RX';
import 'rxjs/add/operator/map';
@Injectable()
export class Service {
apiRoot: string = 'https://jsonplaceholder.typicode.com/comments?'
postId = 1;
data;
constructor(private _http: Http) {
this._http.get(this.apiRoot).subscribe((data) => {
this.data = data.json();
});
}
init() {
return this.data;
}
}
My component code is我的组件代码是
import { Component, OnInit } from '@angular/core'; import { InfiniteService } from './infinite.service'; //import all rxjs/Rx opeartor import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit {
data;
constructor(private _infinteService: InfiniteService) {
this.data = this._infinteService.init();
console.log(this.data);
}
GetPosition($event) {
if ($event == 'Bottom') {
console.log($event);
}
else {
console.log($event);
}
}
ngOnInit() { }
}
why I am not able to get service data in component?为什么我无法在组件中获取服务数据?
Because HTTP its async function!因为 HTTP 它的异步功能! I recommend to use HttpClient, so you don't need to use
map
function!我推荐使用HttpClient,所以你不需要使用
map
函数! You can try that你可以试试
getData(): Observable<any> {
return this._http.get(this.apiRoot)
}
And you just call him in component like that你就这样在组件中调用他
this._infinteService.getData().subscribe(
data => console.log(data)
)
You need to return an Observable, and then you can use subscribe to call request.您需要返回一个 Observable,然后您可以使用 subscribe 来调用请求。 Try that and tell if worked!
试试看,看看是否有效! Thanks
谢谢
Make following changes in your code.在您的代码中进行以下更改。
Service :服务 :
constructor(private _http: Http) {
}
init() {
return this._http.get(this.apiRoot);
}
Component :成分 :
constructor(private _infinteService: InfiniteService) {
this._infinteService.init().subscribe((data) => {
this.data = data;
console.log(this.data);
});
}
You need to use .subscribe
in component call.您需要在组件调用中使用
.subscribe
。
You don't need the .json()
on your data object.您不需要数据对象上的
.json()
。 As of Angular4 the default responseType is JSON and the response data is already parsed for us.从 Angular4 开始,默认的 responseType 是 JSON,并且已经为我们解析了响应数据。
You should return the Observable
from your service and subscribe to it in your component.您应该从您的服务返回
Observable
并在您的组件中订阅它。
The reason for this occur is it will take some time to get the response from the server.发生这种情况的原因是需要一些时间才能从服务器获得响应。 So we can set it in promises.
所以我们可以在promise中设置它。 use the following command to get data in promises.
使用以下命令获取 promise 中的数据。
this._customerService.getCustomers().subscribe(data => this.customers = data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.