网络请求

在uni中可以调用uni.request方法进行请求网络请求。
需要注意的是:在小程序中网络相关的api在使用前需要配置域名白名单。

发送get请求

本地搭建接口文件:
image.png
将里面的数据导入到自己本地数据库中。
image.png
进入该文件夹下的shell,执行 npm i 进行安装。
执行代码: node ./src/app.js 如果出现问题,可以在app.js
中修改数据库用户名和密码。
image.png

  1. <template>
  2. <view>
  3. <button @click="getApi">点击</button>
  4. <view>
  5. 这是列表页面
  6. </view>
  7. <view v-for="(item,index) in list" :key='index'>{{item}}<br></view>
  8. <button type="default" @click="pullDown">下拉刷新</button>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. list:['前端','JAVA','UI','测试','大数据']
  16. }
  17. },
  18. onPullDownRefresh() {
  19. console.log('触发了下拉刷新')
  20. setTimeout(()=>{
  21. this.list = ['JAVA','UI','测试','大数据','前端'] //下拉刷新更新了数据,但是还在不停刷新
  22. uni.stopPullDownRefresh()
  23. },2000) //加了一个延时2秒
  24. },
  25. onReachBottom(){
  26. console.log('页面触底了')
  27. this.list = [...this.list,...['JAVA1','UI1','测试1','大数据1','前端1']]
  28. },
  29. methods: {
  30. pullDown(){
  31. //点击按钮触发下拉刷新
  32. uni.startPullDownRefresh()
  33. },
  34. getApi(){
  35. uni.request({
  36. url:'http://localhost:8082/api/getlunbo',
  37. success(res){
  38. console.log(res)
  39. }
  40. })
  41. }
  42. }
  43. }
  44. </script>
  45. <style>
  46. </style>

image.png