index.vue 轮播图
    在根目录新建until目录;

    1. //封装的方法api.js
    2. //定义变量
    3. const BASE_URL = 'http://localhost:8082'
    4. export const myRequest = (options)=>{
    5. //这是一个方法
    6. return new Promise((resolve,reject)=>{
    7. uni.request({
    8. url:BASE_URL+options.url,
    9. method:options.method || 'GET',
    10. data:options.data || {}, //不传就是为空
    11. success:(res)=> {
    12. if(res.data.status !== 0){
    13. return uni.showToast({
    14. title:'获取数据失败'
    15. })
    16. }
    17. resolve(res)
    18. },
    19. //一旦请求失败
    20. fail:(err)=>{
    21. uni.showToast({
    22. title:'请求接口失败'
    23. })
    24. reject(err)
    25. }
    26. })
    27. })
    28. }
    29. //调用方法
    30. myRequest({
    31. url:'/api/getlunbo'
    32. })

    index.vue代码

    1. <template>
    2. <view>
    3. 首页
    4. </view>
    5. </template>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. swipers:[]
    11. }
    12. },
    13. onLoad() { //监听页面加载
    14. this.getSwiper()
    15. },
    16. methods: {
    17. //获取轮播的数据
    18. // async getSwiper(){
    19. // console.log('获取轮播的数据')
    20. // const res = await uni.request({
    21. // url:'http://localhost:8082/api/getlunbo',
    22. // })
    23. // console.log(res.data)
    24. // }
    25. //获取轮播数据
    26. async getSwiper(){
    27. // console.log('获取轮播的数据')
    28. // uni.request({
    29. // url:'http://localhost:8082/api/getlunbo',
    30. // success:res=>{
    31. // console.log(res.data.message)
    32. // if(res.data.status !== 0){
    33. // return uni.showToast({
    34. // title:'获取数据失败'
    35. // })
    36. // }
    37. // this.swipers = res.data.message
    38. // }
    39. // })
    40. const res =await this.$myRequest({
    41. url:'/api/getlunbo'
    42. })
    43. this.swipers = res.data.message
    44. }
    45. }
    46. }
    47. </script>
    48. <style>
    49. </style>

    程序中的async和 await 是成对使用的。
    因为要全局使用

    1. import Vue from 'vue'
    2. import App from './App'
    3. //全局方法
    4. import { myRequest} from './util/api.js'
    5. Vue.prototype.$myRequest = myRequest //全局方法
    6. Vue.config.productionTip = false
    7. App.mpType = 'app'
    8. const app = new Vue({
    9. ...App
    10. })
    11. app.$mount()