c# - 我在 ionic 中使用了 api,但我遇到了一些问题。你能解释一下我们如何在 ionic 中使用 api 吗?
问题描述
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);
}}
解决方案
在您拥有的 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);
最后两行:
.then(onfulfilled:res=>this.requestObject =res.data)
和
.catch(onrejected:err=>this.requestObject=err)
应该意味着(我有一段时间没有接触过角度)如果请求没有错误,则调用onfulfilled,但如果有错误,则调用onrejected。
您需要在代码中定义这两个函数,以便 Angular 在适当的时候调用它们。
推荐阅读
- java - 实例变量和实例方法在java中存储在哪里?
- r - 如何使用 R 调整 x 轴字体的大小?
- python - 将 Pandas DataFrame 列转换为行
- asp.net - Bootstrap btn-group-justified
- android - 需要一种向此 android xamarin 示例添加按钮的方法
- r - 使用 AzureML 和 randomForest() 时出错
- gcc - 为什么 GCC 的 AVX 速度较慢,而 LLVM 的速度更快?
- laravel - Laravel 文件上传不工作,不知道为什么
- python-3.x - 项目中跨文件的全局 DataFrame
- r - 使用参数调用 R Shiny App