• uniapp 配置网络请求并使用请求轮播图


    由于平台的限制,小程序项目中不支持 axios,而且原生的 wx.request() API 功能较为简单,不支持拦截器等全局定制的功能。因此,建议在 uni-app 项目中使用 @escook/request-miniprogram 第三方包发起网络数据请求。

    官方文档@escook/request-miniprogram - npm (npmjs.com)

    • 1、安装 相应的包

    1. 1、安装一个npm包管理文件:
    2. npm init -y
    3. 2、安装我们这个网络请求第三方工具
    4. npm i @escook/request-miniprogram

    • 2、导入 $http 对象

    1. import { $http } from '@escook/request-miniprogram'
    2. uni.$http = $http

    • 3、配置 请求根路径、请求拦截器

    1. // 2、配置请求根路径
    2. $http.baseUrl = 'https://api-hmugo-web.itheima.net'
    3. // 3、请求开始之前做一些事情
    4. $http.beforeRequest = function (options) {
    5. uni.showLoading({
    6. title: '数据加载中...',
    7. })
    8. }
    9. // 4、请求完成之后做一些事情
    10. $http.afterRequest = function () {
    11. uni.hideLoading()
    12. }

    • 4、使用 网络请求

    先在pages下建立需要的文件夹,再在这些文件夹上右键点击新建页面,创建就行 

    1. <template>
    2. <view>
    3. <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
    4. <swiper-item v-for="(item, i) in swiperList" :key="i">
    5. <view class="swiper-item">
    6. <image :src="item.image_src">image>
    7. view>
    8. swiper-item>
    9. swiper>
    10. view>
    11. template>
    12. <script>
    13. export default {
    14. data() {
    15. return {
    16. // 1. 轮播图的数据列表,默认为空数组
    17. swiperList: [],
    18. }
    19. },
    20. onLoad() {
    21. // 2. 在小程序页面刚加载的时候,调用获取轮播图数据的方法
    22. this.getSwiperList()
    23. },
    24. methods: {
    25. // 3. 获取轮播图数据的方法
    26. async getSwiperList() {
    27. // 3.1 发起请求 【把data从res{data}中解构出来】
    28. const {
    29. data: res
    30. } = await uni.$http.get('/api/public/v1/home/swiperdata')
    31. console.log(res);
    32. // 3.2 请求失败
    33. if (res.meta.status !== 200) {
    34. return uni.showToast({
    35. title: '数据请求失败!',
    36. duration: 1500,
    37. icon: 'none',
    38. })
    39. }
    40. // 3.3 请求成功,为 data 中的数据赋值
    41. this.swiperList = res.message
    42. },
    43. },
    44. }
    45. script>
    46. <style lang="scss">
    47. swiper {
    48. height: 330rpx;
    49. .swiper-item,
    50. image {
    51. width: 100%;
    52. height: 100%;
    53. }
    54. }
    55. style>

  • 相关阅读:
    基于FPGA的PSRAM接口设计与实现
    20231012-黑马Vue学习笔记-第一天
    LeetCode 0827. 最大人工岛
    逆向分析:基于 JS 字节码的保护技术
    AI为方,产业为向:京东云数字人的技术攀爬
    UNITY—2D游戏制作入门!
    Oracle 19c RAC 为了避免一些不必要的bug设定参数
    JSON介绍
    入门Python,看完这篇就行了!
    当前系统并无桌面环境,或无显示器,无法显示远程桌面,您需要自行安装X11桌面环境,或者使用终端文件功能
  • 原文地址:https://blog.csdn.net/weixin_49931650/article/details/132568577