日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

uni-app 使用escook/request-miniprogram插件發(fā)請求

作者:調(diào)調(diào)啊 更新時間: 2022-11-08 編程語言

一 ,?npm?install?@escook/request-miniprogram

用這個插件發(fā)請求的優(yōu)點就是可以直接在main.js中設(shè)置請求攔截器和響應(yīng)攔截器 , 還有根路徑 , 比較方便

二 , main.js中


// 導(dǎo)入發(fā)請求的包,并掛載到uni身上
import {
	$http
} from '@escook/request-miniprogram'

uni.$http = $http


// 請求根路徑 
$http.baseUrl = '/api' 


//請求攔截器
$http.beforeRequest = function(config) {
	

}

//響應(yīng)攔截器
$http.afterRequest = function(response) {
	console.log(response)

	if (response.data.code == '0001') {
		uni.showToast({
			title: '您還沒有登錄喔',
			duration: 2000,
			icon: "none"
		});

		setTimeout(function() {
			uni.navigateTo({
				url: '../login/login'
			});
		}, 1000)


	} else {
		return
	}
}

二 , 需要發(fā)請求的.vue頁面中

uni.$http.post('請求路徑', 參數(shù));

methods: {
			// 登錄
			async formSubmit(e) {
                //參數(shù)
				const params = {
					username: e.detail.value.username,
					password: e.detail.value.password,	
				};
                // 開啟loading
				uni.showLoading({
					title:"登錄中"
				})
               //發(fā)送請求
				const {data: res} = await uni.$http.post('/login', params);
				
				if (res.code !== '0') {
					return uni.showToast({
						title: '用戶名或密碼錯誤',
						duration: 2000,
						icon: 'none'
					});
					return;
				} else {
					uni.showToast({
						title: '登錄成功',
						duration: 2000,
						icon: 'none'
					});    
				}
			}
		}

原文鏈接:https://blog.csdn.net/weixin_49577940/article/details/121956987

欄目分類
最近更新