uniapp封装,封装狂魔,2024年上半年最接地气的前端面经

文章详细介绍了如何在Vue项目中封装HTTP请求函数,如getSwiper、getType等,以及如何使用这些封装的函数进行页面间的导航和数据交互,展示了在组件中调用这些接口的方法和结构。
摘要由CSDN通过智能技术生成

complete() {

uni.hideLoading()

}

})

})

};

// 2 封装成功提示 使用:toast({}) 或 toast({title:“请求失败”,icon:“error”})

export const toast = ({

title = ‘请求成功’,

icon = ‘succes’,

duration = 1500,

}) => {

uni.showToast({

title,

duration,

icon,

})

};

//3 封装跳转 函数形式 这种可以直接挂载到vue原型上使用的时候不需要引入

// export function router ({url=‘’,type=‘navigateTo’,}) {

// uni[type]({

// url

// })

// };

//3 封装跳转 形参形式 router({url=‘xx’,type=‘xx’})

export const router = ({

url = ‘’,

type = ‘navigateTo’

}) => {

uni[type]({

url

})

};

utils/api.js


import {

http

} from ‘./http.js’

// 获取轮播图

export const getSwiper = async () => http({

url: ‘/home/swiperdata’

})

// 获取分类导航

export const getType = async () => http({

url: ‘/home/catitems’

})

// 获取楼层

export const getItem = async () => http({

url: ‘/home/floordata’

})

// 获取分类数据

export const getCates = async () => http({

url: ‘/categories’

})

// 搜索数据

export const getSearchList = async (key) => http({

url: ‘/goods/search’,

data: {

…key

}

})

在页面使用

=====

<navigator class=“nav” :class=“‘nav’ + index” :url=“‘/pages’ + it.navigator_url” :key=“index”

v-for=“(it, index) in item.product_list”>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值