서비스를 사용하여 GET
요청을 만들려고하면이 서비스 (data.service.ts
)에서 응답을 성공적으로 인쇄 할 수 있지만 컴포넌트가 정의되지 얻는다 (send.component.ts
)Ionic2 : HttpClient가있는 서비스에서 get 요청을 구성 요소에서 가져 오지 않음
data.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map']
@Injectable()
export class DataService {
getBusiness(){
this.http.get('http://127.0.0.1/business').subscribe(data => {
this.BUSINESS = data;
return this.BUSINESS;
});
}
constructor(private http:HttpClient){}
}
send.component.ts
import { DataService } from '../../app/data.service';
@Component({
selector: 'page-send',
templateUrl: 'send.html'
})
export class SendPage {
constructor(private dataService:DataService) {
this.title = navParams.get('title');
console.log(dataService.getBusiness())
}
}
JSON 응답 :
[{
"_id": "5a0cf90cf3de893b000b8e3b",
"name": "business2",
"boxes": ["dasdsadsdasbox", "dsadajhhgbox"],
"users": ["user2"],
"deleted": false
}, {
"_id": "5a0cf90cf3de893b321321",
"name": "business1",
"boxes": ["dasds321321box", "ds321321hgbox"],
"users": ["user1"],
"deleted": false
}]
왜 그런 일이 수행하는 방법과 그것을 해결하기 위해?
감사합니다. 코드 아래
가능한 중복 변수에 할당] (https://stackoverflow.com/questions/43337354/ionic2-returning-a-value-from-http-request-and-ass) ign-it-to-variable) –