import { Component, OnInit } from '@angular/core';
import { HTTP } from '@ionic-native/http/ngx';
@Component({
selector: 'app-home',
templateUrl:'./home.page.html',
styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {
requestObject:any=null;
constructor(private http: HTTP) {}
ngOnInit(){}
getRequest({
this.http.get(url:'https://jsonplaceholder.typicode.com/todos/1',
parameters:{},
headers:{}
).then(onfulfilled:res=>this.requestObject =res.data)
.catch(onrejected:err=>this.requestObject=err);
}}
答案 0 :(得分:0)
在您拥有的getRequest代码上
getRequest({
this.http.get(url:'https://jsonplaceholder.typicode.com/todos/1',
parameters:{},
headers:{}
).then(onfulfilled:res=>this.requestObject =res.data)
.catch(onrejected:err=>this.requestObject=err);
最后2行:
.then(onfulfilled:res=>this.requestObject =res.data)
和
.catch(onrejected:err=>this.requestObject=err)
应表示(我已经有一段时间没有碰到角了),如果请求没有错误,您可以致电 onfulfilled ,但如果您有错误,则可以致电 onrejected 。
您需要在代码中定义这两个函数,以便Angular在适当的时候调用它们。