当前位置:   article > 正文

uniapp 学习笔记八 使用promise方法封装post和get和put_export const $post

export const $post

 

homee.vue

  1. <template>
  2. <scroll-view class="scroll-cont" :scroll-into-view="topItem" @scroll="handleScroll" scroll-y="true" scroll-with-animation="true">
  3. <view>
  4. <view id="top"></view>
  5. <!-- 自定义导航栏 -->
  6. <nav-custom></nav-custom>
  7. <!-- 轮播 -->
  8. <swiper class="banner" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
  9. <swiper-item v-for="(index,item) in 3">
  10. <image src="../../static/logo.png" mode=""></image>
  11. </swiper-item>
  12. </swiper>
  13. <home-title
  14. title="本季推荐"
  15. en-title="Seasonal Recommend"
  16. en-tit="Seasonal"
  17. ></home-title>
  18. <scroll-view scroll-x="true" >
  19. <view class="scroll-inner">
  20. <image src="../../static/logo.png" mode="heightFix"></image>
  21. <image src="../../static/logo1.png" mode="heightFix"></image>
  22. <image src="../../static/logo.png" mode="heightFix"></image>
  23. <image src="../../static/logo1.png" mode="heightFix"></image>
  24. </view>
  25. </scroll-view>
  26. <home-title
  27. title="法式经典"
  28. en-title="French Classic"
  29. en-tit="French"
  30. ></home-title>
  31. <image class="classify" src="../../static/logo.png" mode=""></image>
  32. <view class="flex flex-wrap padding-sm justify-between">
  33. <good-item v-for="(item,index) in 4"></good-item>
  34. </view>
  35. <view class="back-top" v-if="isShowGoTop" @tap="handleBackTop">
  36. <text class="iconfont icon-huidingbu"></text>
  37. </view>
  38. </view>
  39. </scroll-view>
  40. </template>
  41. <script>
  42. // 局部引入封装的 promise $http
  43. import {$http} from '@/utils/request.js'
  44. export default {
  45. data() {
  46. return {
  47. isShowGoTop:false,
  48. topItem:'' //返回顶部的标记点
  49. }
  50. },
  51. methods: {
  52. handleScroll(ev){
  53. // console.log(ev.detail);
  54. let {scrollTop} = ev.detail
  55. this.isShowGoTop = scrollTop >600
  56. this.topItem = '' //重置定位返回点
  57. },
  58. handleBackTop(){
  59. this.topItem = 'top'
  60. }
  61. },
  62. onLoad() {
  63. // 方法一
  64. /* uni.request({
  65. url: 'https://m25ygr******ed.com/1.1/classes/classify',
  66. method: 'GET',
  67. header:{
  68. "X-LC-Id": "**********",
  69. "X-LC-Key": "**********",
  70. "Content-Type": "application/json",
  71. },
  72. data: {},
  73. success: res => {
  74. console.log(res);
  75. },
  76. fail: () => {},
  77. complete: () => {}
  78. }); */
  79. // 方法二 使用封装的到promis里面的 $http方法
  80. /* $http('/1.1/classes/classify').then(res=>{
  81. console.log(res);
  82. }) */
  83. // 方法三 使用全局封装的 $get方法
  84. this.$get('/1.1/classes/classify').then(res=>{
  85. console.log(res);
  86. })
  87. }
  88. }
  89. </script>
  90. <style lang="scss">
  91. .back-top{
  92. height: 100upx;
  93. width: 100upx;
  94. background-color: #fff;
  95. border-radius: 50%;
  96. box-shadow: 0 0 10upx 4upx rgba(0, 0, 0, 0.4);
  97. position: fixed;
  98. bottom: 40upx;
  99. right: 20upx;
  100. text-align: center;
  101. line-height: 100upx;
  102. }
  103. .classify{
  104. height: 380upx;
  105. width: 100%;
  106. }
  107. .scroll-inner{
  108. white-space: nowrap;
  109. image{
  110. height: 290upx;
  111. width: auto;
  112. }
  113. }
  114. .banner{
  115. height: 1000upx;
  116. swiper-item{
  117. height: 1000upx;
  118. }
  119. image{
  120. width: 100%;
  121. height:1000upx
  122. }
  123. }
  124. .scroll-cont{
  125. height: 100vh;
  126. }
  127. </style>

main.js

局部挂载和全局挂载promise

单个挂载和批量挂载 promise

  1. import App from './App'
  2. // #ifndef VUE3
  3. import Vue from 'vue'
  4. Vue.config.productionTip = false
  5. App.mpType = 'app'
  6. const app = new Vue({
  7. ...App
  8. })
  9. // 全局引入uView主JS库
  10. import uView from '@/uni_modules/uview-ui'
  11. Vue.use(uView)
  12. // 全局引入自定义组件
  13. import NavCustom from '@/components/nav-custom.vue'
  14. Vue.component('nav-custom',NavCustom)
  15. import HomeTitle from '@/components/home-title.vue'
  16. Vue.component('home-title',HomeTitle)
  17. import GoodItem from '@/components/goods-item.vue'
  18. Vue.component('good-item',GoodItem)
  19. // 挂载全局异步请求方法(单个挂载)
  20. /* import {$http,$get,$post,$put} from '@/utils/request.js'
  21. Vue.prototype.$http = $http
  22. Vue.prototype.$get = $get
  23. Vue.prototype.$post = $post
  24. Vue.prototype.$put = $put */
  25. // 挂载全局异步请求方法(批量挂载request.js里面的所有方法)
  26. import * as request from '@/utils/request.js'
  27. for (let key in request) {
  28. Vue.prototype[key] = request[key]
  29. }
  30. app.$mount()
  31. // #endif
  32. // #ifdef VUE3
  33. import { createSSRApp } from 'vue'
  34. export function createApp() {
  35. const app = createSSRApp(App)
  36. return {
  37. app
  38. }
  39. }
  40. // #endif

request.js

  1. let baseURL = 'https://m25y********ared.com'
  2. export const $http = function(url,method='GET',data={}){
  3. return new Promise((resolve,reject)=>{
  4. uni.request({
  5. url: baseURL+url,
  6. method,
  7. header:{
  8. "X-LC-Id": "**********",
  9. "X-LC-Key": "**********",
  10. "Content-Type": "application/json",
  11. },
  12. data,
  13. success: (res) => {
  14. resolve(res.data)
  15. },
  16. fail: (err) => {
  17. reject(err)
  18. },
  19. complete: () => {}
  20. });
  21. })
  22. }
  23. export const $get = function(url,data={}){
  24. return $http(url,'GET',data)
  25. }
  26. export const $post = function(url,data={}){
  27. return $http(url,'POST',data)
  28. }
  29. export const $put = function(url,data={}){
  30. return $http(url,'PUT',data)
  31. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/117051?site
推荐阅读
相关标签
  

闽ICP备14008679号