当前位置: 首页 > news >正文

UNIAPP调用API接口

API:开发者可以通过这些接口与其它程序进行交互,获取所需数据或者执行指定操作。

网络请求 API:

UniApp 中内置了网络请求 API,方便调用

uni.request
uni.uploadFile

uni.request 接口主要用于实现网络请求GET 和 POST 是使用最普遍的两种请求方式。

该接口支持的请求方式有:GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT。

参数名类型是否必填作用
urlString请求的 URL 地址
methodString请求的方式,支持 GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT
headerObject需要设置的请求头部信息
dataObject/String请求的数据
dataTypeString返回值的数据类型,支持 json、text、default
responseTypeString响应类型,支持 text、arraybuffer、blob
successFunction请求成功后的回调函数
failFunction请求失败后的回调函数
completeFunction请求完成后的回调函数
uni.request({url: 'https://api.example.com/login', method: 'POST',  header: {'content-type': 'application/json'},  data: {username: 'example',password: 'example123'},  success: res => {console.log(res.data)},  fail: error => {console.log(error)}
})

uni.uploadFile 接口主要用于上传文件

参数名类型是否必填作用
urlString请求的 URL 地址
filePathString要上传的文件路径,仅支持本地路径
nameString上传文件的名字
headerObject需要设置的请求头部信息
formDataObject需要上传的额外参数
successFunction请求成功后的回调函数
failFunction请求失败后的回调函数
completeFunction请求完成后的回调函数
uni.uploadFile({url: 'https://api.example.com/upload', filePath: '/path/to/file',name: 'file', header: {'content-type': 'multipart/form-data'}, formData: {'name': 'example'},success: res => {console.log(res.data)},fail: error => {console.log(error)}
})

路由 API:

uni.navigateTo

uni.redirectTo

uni.navigateTo 接口是用于跳转到新页面的方法,并传递参数

通过这个接口,我们可以实现跳转到新页面,并传递参数。

参数名类型是否必填作用
urlString要跳转的页面路径,支持相对路径和绝对路径
successFunction跳转成功后的回调函数
failFunction跳转失败后的回调函数
completeFunction跳转完成后的回调函数
uni.navigateTo({url: '/pages/detail/detail?id=1',success: res => {console.log(res)},fail: error => {console.log(error)}
})

uni.redirectTo 接口是用于关闭当前页面跳转到新页面的方法

参数名类型是否必填作用
urlString要跳转的页面路径,支持相对路径和绝对路径
successFunction跳转成功后的回调函数
failFunction跳转失败后的回调函数
completeFunction跳转完成后的回调函数
uni.redirectTo({url: '/pages/index/index',success: res => {console.log(res)},fail: error => {console.log(error)}
})

Storage API

一些数据需要本地存储,以便在下次启动应用程序时能够快速访问到

UniApp 提供了 Storage API,用于本地存储数据。

方法名参数作用
uni.setStoragekey,value将数据存储在本地缓存中
uni.getStoragekey从本地缓存中获取指定 key 对应的内容
uni.removeStoragekey从本地缓存中删除指定 key
uni.clearStorage清空本地缓存
// 存储数据
uni.setStorage({key: 'username',data: 'example',success: function () {console.log('数据存储成功')}
})// 获取数据
uni.getStorage({key: 'username',success: function (res) {console.log(res.data)}
})// 删除数据
uni.removeStorage({key: 'username',success: function () {console.log('数据删除成功')}
})

网络请求、路由、Storage 存储、组件、插件、生命周期

http://www.lryc.cn/news/138721.html

相关文章:

  • 理解 Delphi 的类(五) - 认识类的继承
  • mybatis概述及搭建
  • DNDC模型---土壤碳储量、温室气体排放、农田减排、土地变化、气候变化中的应用
  • Android studio 2022.3.1 鼠标移动时不显示快速文档
  • 五度易链最新“产业大数据服务解决方案”亮相,打造数据引擎,构建智慧产业!
  • 简述hive环境搭建
  • 小米AI音箱联网升级折腾记录(解决配网失败+升级失败等问题)
  • tensorRT安装
  • 电脑重装+提升网速
  • Modelica由入门到精通—为什么要学习Modelica语言
  • opencv 进阶20-随机森林示例
  • Spring Boot进阶(58):集成PostgreSQL数据库及实战使用 | 万字长文,超级详细
  • Java | 使用ServerSocket查找TCP可用端口
  • 【深入浅出C#】章节 9: C#高级主题:LINQ查询和表达式
  • 【Git】git clone --depth 1 浅克隆
  • 搭建 Gitlab
  • CTFhub-sqli注入-报错注入
  • 中国人民大学与加拿大女王大学金融硕士让金融界短暂迷茫的你发现新的方向
  • PHPEXCEL 导出excel
  • Elasticsearch简介及安装
  • Python 密码破解指南:10~14
  • Spring、SpringMVC、SpringBoot三者的区别
  • 探索PDF校对:为何这是现代数字文档的关键步骤
  • linux 同时kill杀死多进程实践
  • 全流程R语言Meta分析核心技术
  • 打家劫舍00
  • ​LeetCode解法汇总1267. 统计参与通信的服务器
  • Go 语言在 Windows 上的安装及配置
  • 如何在不使用任何软件的情况下将 PDF 转换为 Excel
  • 【C语言】动态内存管理(malloc,free,calloc,realloc)-- 详解