uniapp 网络请求

ゞ 浴缸里的玫瑰 2021-07-24 11:27 594阅读 0赞
  1. 使用箭头函数避免this指向,回调中的this不是当前页面的this
  2. 回调不是箭头函数,this的指向不是当前Vue实例
  3. 1get
  4. uni.request({
  5. url:"路径",
  6. success:(res)=>{
  7. res为返回的数据
  8. }
  9. })
  10. 2post
  11. uni.request({
  12. url:"路径",
  13. data:{
  14. 参数键值对
  15. },
  16. success:(res)=>{
  17. res为返回的数据
  18. }
  19. })

文档

代码示例:

  1. <template>
  2. <view>
  3. <view class='box'>
  4. { { msg}}
  5. </view>
  6. <text class='iconfont icon-shipin '></text>
  7. <text class='iconfont'></text>
  8. <text v-if="true" v-html="hhh">wwww</text>
  9. <button type="primary" @click="get">按钮</button>
  10. <view v-for="(item,index) in list" :key="index">
  11. <view>{ { item}}</view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default{
  17. data()
  18. {
  19. return{
  20. msg:'数据',
  21. hhh:'ww',
  22. list:[1,2,3,4,5,6,7,8]
  23. }
  24. },
  25. onReachBottom()
  26. {
  27. this.list.push(10);
  28. },
  29. methods:{
  30. get()
  31. {
  32. uni.request({
  33. // method:"POST",
  34. url:"http://api.tianapi.com/txapi/ncov/index",
  35. data:{
  36. key:"4a8edfc8ac5eae9b0c5bf08157abba96"
  37. },
  38. success:function(res){
  39. console.log(res);
  40. }
  41. })
  42. }
  43. },
  44. }
  45. </script>
  46. <style scoped>
  47. @import url("../css/a.css");
  48. .box{
  49. height: 1000rpx;
  50. width: 375rpx;
  51. background-color: #4CD964;
  52. }
  53. .box1{
  54. background-color: #007AFF;
  55. }
  56. </style>

发表评论

表情:
评论列表 (有 0 条评论,594人围观)

还没有评论,来说两句吧...

相关阅读