ajax请求


研究这个研究了好久。。。。不过总算是研究出来了。。。

我们先定义了一个全局函数,这个函数是一个异步的函数

async requests (that, url, data, type) {
	try {
		// 判断请求类型
		if (type === 'get') {
			return (await that.$axios.get(url, { params: data })).data
		} else if (type === 'post') {
			return (await that.$axios.post(url, data)).data
		} else {
			return null
		}
	} catch (e) {
		return null
	}
}

我这里是直接返回请求到的结果了。主函数我们可以这样用

this.tools.requests(this, this.G.SERVER + '/api/v1/post/get/category', {}, 'get').then((data) => {
	if (data.code === 100) {
		this.parent = data.data.parent
		this.child = data.data.child
	}
})

这个函数变成了一个异步函数


文章作者: 小游
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小游 !
  目录