学习Uni-app开发小程序Day15

今天学习了uni-app中的网络请求

网络请求

uni.request(OBJECT)

这里是发起网络请求

这是常用的一种方式

这里有个疑问:为什么success后面使用的是res?

原来这个res只是一个名称,这里可以使用其他的,不是一定要用res

html 复制代码
function request() {
	uni.request({
		url: "https://jsonplaceholder.typicode.com/posts",
		success: res => {
			console.log(res);
			arrs.value = res.data
		}
	})
}

这是第二种写法

html 复制代码
// 第二种方式,不使用success了,使用的是then,这里也不懂为什么要这么使用
// 这里查询了网络,原因是这里使用的访问网络接口是使用的Promises的方式,而
// then是Promises下的一个方法,允许指定回调函数,在异步任务完成后调用。
	function request() {
		uni.request({
			url: "https://jsonplaceholder.typicode.com/posts"
		}).then(res => {
			arrs.value = res.data
		});
	}

还有第三种用法

html 复制代码
// 第三种是使用异步的,但是有个关键的是 async和await,还是老问题,为什么要这么用?
// 函数前的关键字 async 使函数返回 promise,函数前的关键字 await 使函数等待 promise
	async function request() {
	 	let res= await	uni.request({
				url: "https://jsonplaceholder.typicode.com/posts"
			})
			arrs.value = res.data
	 	}

这里提出了几个疑问,自己并不清楚,在学习视频中,有给这种学习资料的,这里不做过多的描述

在哔哩哔哩中搜索:ES6 Promise的用法,ES7 async/await异步处理同步化,异步处理进化史,这里有使用的详解。

上面的接口是在学习文档中有提供,大家可以看看。
学习uni-app源码和学习文档

这里提一下,接口测试工具,使用的是ApiPost,也可以使用PostMan。

参数的使用,下面按照上面的三种情况,各个都做个实验,代码中有详细描述

html 复制代码
<template>
	<view class="">

	</view>
</template>

<script setup>
	uni.showLoading()

	// function request() {
	// 	uni.request({
	// 		url: "https://jsonplaceholder.typicode.com/posts",
	// 		data:{
	// 			id:'100'
	// 		},
	// 		method:'GET',
	// 		timeout:1000,
	// 		header:{
	// 			'to-header':'holle',
	// 			'content-type':'text/plain'
	// 		},

	// 	}).then(res => {
	// 		// console.log(res);
	// 		// arrs.value = res.data
	// 		// 这里设置了取消loading后,是没有效果的,
	// 		uni.hideLoading();
	// 		console.log(res.errMsg);
	// 	});
	// }

	function request1() {
		uni.request({
			url: "https://jsonplaceholder.typicode.com/posts",
			data: {
				id: '100'
			},
			method: 'GET',
			timeout: 1000,
			header: {
				'to-header': 'holle',
				'content-type': 'text/plain'
			},
			success: res => {
				console.log(res);
			},
			fail: err => {
				console.log(err);
			},
			// 使用这种方式也可以实现,感觉这种写法是错误的,但能实现效果
			complete() {
				uni.hideLoading()
				console.log(12);
			}
			// 下面是官方的写法,建议按照这种写法
			// complete: () => {
			// 	uni.hideLoading();
			// }
		})
	}

	async function request() {
		let res = await uni.request({
			url: "https://jsonplaceholder.typicode.com/posts",
			data: {
				id: '100'
			},
			method: 'GET',
			timeout: 1000,
			header: {
				'to-header': 'holle',
				'content-type': 'text/plain'
			},
			complete() {
				uni.hideLoading()
			},
			fail: (err) => {
				console.log(err);
			}
		})
		// arrs.value = res.data
	}

	request();
</script>

<style lang="scss" scoped>

</style>

上面的是根据三种情况,写的接口调用成功、失败、不论成功是失败都进入的方法。这里是做个测试,把timeout 的时间修改成1000ms,默认是60000ms。content-type 也是测试的,默认是application/json,data 是在传递参数的时候写的,使用的是数组的方式。

这就是网路请求过程中常用的参数,其他的可以看下文档,在以后使用的时候,查看文档使用。

初步的基础今天就学习完了,下来开始要做案例了,在案例中有新的问题或者新的知识点,我都会记录的,也算检验下近期学习的成果。
不忘初心、方得始终!

相关推荐
百思可瑞教育3 小时前
uni-app 根据用户不同身份显示不同的tabBar
vue.js·uni-app·北京百思可瑞教育·北京百思教育
老华带你飞5 小时前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
项目題供诗5 小时前
微信小程序开发教程(八)
微信小程序·小程序
茯苓gao8 小时前
STM32G4 电流环闭环
笔记·stm32·单片机·嵌入式硬件·学习
easy20208 小时前
机器学习的本质:从跑模型到真正解决问题
笔记·学习·机器学习
Q_Q19632884759 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
普蓝机器人10 小时前
AutoTrack-IR-DR200仿真导航实验详解:为高校打造的机器人学习实践平台
人工智能·学习·机器人·移动机器人·三维仿真导航
百思可瑞教育11 小时前
使用UniApp实现一个AI对话页面
javascript·vue.js·人工智能·uni-app·xcode·北京百思可瑞教育·百思可瑞教育
不想吃饭e11 小时前
在uniapp/vue项目中全局挂载component
前端·vue.js·uni-app