uniapp 网络请求
使用箭头函数避免this指向,回调中的this不是当前页面的this
回调不是箭头函数,this的指向不是当前Vue实例
1、get
uni.request({
url:"路径",
success:(res)=>{
res为返回的数据
}
})
2、post
uni.request({
url:"路径",
data:{
参数键值对
},
success:(res)=>{
res为返回的数据
}
})
文档
代码示例:
<template>
<view>
<view class='box'>
{ { msg}}
</view>
<text class='iconfont icon-shipin '></text>
<text class='iconfont'></text>
<text v-if="true" v-html="hhh">wwww</text>
<button type="primary" @click="get">按钮</button>
<view v-for="(item,index) in list" :key="index">
<view>{ { item}}</view>
</view>
</view>
</template>
<script>
export default{
data()
{
return{
msg:'数据',
hhh:'ww',
list:[1,2,3,4,5,6,7,8]
}
},
onReachBottom()
{
this.list.push(10);
},
methods:{
get()
{
uni.request({
// method:"POST",
url:"http://api.tianapi.com/txapi/ncov/index",
data:{
key:"4a8edfc8ac5eae9b0c5bf08157abba96"
},
success:function(res){
console.log(res);
}
})
}
},
}
</script>
<style scoped>
@import url("../css/a.css");
.box{
height: 1000rpx;
width: 375rpx;
background-color: #4CD964;
}
.box1{
background-color: #007AFF;
}
</style>
还没有评论,来说两句吧...