在請求后端的時候,需要帶請求頭去訪問,在uview里寫了http請求可以傳遞的參數(shù):詳情參考:Http請求 | uView 2.0 - 全面兼容nvue的uni-app生態(tài)框架 - uni-app UI框架
實戰(zhàn)代碼示例:
//現(xiàn)在這個請求接口做的是給后端發(fā)送數(shù)據(jù)
uni.$u.http.post('/后臺接口/***', 第二個參數(shù)(后臺需要傳遞的參數(shù)), {
header: {
'Content-Type': '需要攜帶的請求頭'
},
dataType: 'json'
}).then(res => {
// console.log('要傳遞了')
//在驗證通過,后臺數(shù)據(jù)傳回來的時候,需要提示用戶驗證通過
uni.showToast({
title: "驗證通過!",
icon: "none"
});
that.$refs['resetBtn'].$dispatch('Form', 'uni-form-reset', {
type : 'reset'
})
//因為forData里的fynum原本默認(rèn)就是0,所以這里要手動給他設(shè)置回0,順便把listid里面的數(shù)值也清空
that.formData.fynum = 0;
that.listid = [{
id: 1,
carmodel:'',
num:0
}]
})
?在項目中,請求接口的時候,要做的是渲染列表,有的時候要給后臺發(fā)一個參數(shù),在點擊某個數(shù)據(jù)的時候,會發(fā)一個數(shù)據(jù)來告訴后臺你要查看的是哪一條數(shù)據(jù)。示例:
advertisementPost() {
let advertisementData = {
isrecommend: 1
}
uni.$u.http.post(
'/這個地方是接口/',
//第二個數(shù)據(jù)是參數(shù),就是當(dāng)前點擊的時候要傳進后端的參數(shù)
advertisementData
).then((res) => {
//這里把獲取到的參數(shù)賦值
this.advertisementget = res;
})
},
在頁面跳轉(zhuǎn)的時候,需要攜帶參數(shù)去下一個頁面,比如在點擊一個按鈕的時候,跳轉(zhuǎn)到其他頁面
<template>
<view>
<view
@click="onJump('/這里是路徑/port?codeId='+codeid+'&codename='+codename)">點我跳轉(zhuǎn)
</view>
</view>
</template>
js調(diào)用跳轉(zhuǎn):
onJump(url) {
uni.navigateTo({
url: url
})
}
這個時候,跳轉(zhuǎn)的時候就會攜帶當(dāng)前頁面的參數(shù),去下一個頁面。
在下一個頁面可以這樣接收:
注意:要在data里面聲明??!文章來源:http://www.zghlxwxcb.cn/news/detail-580455.html
onLoad(e) {
//這個e里面,就包含了跳轉(zhuǎn)時傳的參數(shù),可以在這里操作e里面的參數(shù),比如說賦值,在調(diào)用接口
//去傳參數(shù),這里給一個參考
console.log(e)
this.codeid = e.codeid
this.codename = e.codename
}
methods:{
getcode(){
uni.$u.http.post('/接口/',{
//第二個參數(shù)
//后端要的參數(shù)名字:this.codeid 比如:
codeId : this.codeid,
codename : this.codename
}).then(res=>{
console.log(res)
})
}
}
持續(xù)更新,如果有錯誤的地方,望眾位大佬說明文章來源地址http://www.zghlxwxcb.cn/news/detail-580455.html
到了這里,關(guān)于uniapp開發(fā)筆記(1)——uview API接口請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!