uniapp自定義頁面跳轉(zhuǎn)loading的實現(xiàn)代碼
前言
在開發(fā)uniapp項目時,頁面切換的過程中,經(jīng)常需要添加loading效果以提高用戶體驗。uniapp自帶的loading效果無法自定義,所以想著自己實現(xiàn)一個。
實現(xiàn)思路
- 封裝一個全局loading組件,放置插槽和loading效果,使用
v-if
或者v-show
去進行切換。 - 將切換的狀態(tài)isLoading放置在
app.vue
中的globalData
里面來進行狀態(tài)控制 - 使用
uni.$on
和uni.$emit
來完成切換即可
實現(xiàn)代碼
globalData
app.vue
我們需要統(tǒng)一管理頁面切換時的loading狀態(tài),以便在其他頁面中使用。
<script> export default { globalData: { loading: false } } </script>
pageLoading組件實現(xiàn)
src/components/pageLoading/index.vue
在created中監(jiān)聽 pageSwitch 事件, res就是你傳遞的參數(shù),true就是開啟loading效果
<template> <view class="container"> <slot v-if="!loading"></slot> <view v-else class="center"> <!-- loading組件可以自己實現(xiàn), 不想自己寫就找個網(wǎng)站cv一下 --> <!-- <loading></loading> --> 加載中... </view> </view> </template> <script> export default { name: 'pageLoading', data() { return { loading: false } }, created() { this.loading = getApp().globalData.loading uni.$on('pageSwitch', res => { this.loading = res getApp().globalData.loading = res }) } } </script> <style lang="scss" scoped> .center { position: fixed; top: 0; left: 0; right: 0; bottom: 0; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background-color: rgba(0, 0, 0, 0.6); } </style>
使用
在跳轉(zhuǎn)的時候開啟loading效果,跳轉(zhuǎn)到對應頁面之后再去手動關閉即可,或者是在某個請求完成之后去關閉
<template> <page-loading> <button @click="jump">跳轉(zhuǎn)</button> </page-loading> </template> <script> // 嫌麻煩注冊個全局組件 import pageLoading from '../../components/pageLoading/index.vue' export default { components: { pageLoading }, methods: { jump() { uni.$emit('pageSwitch', true) uni.switchTab({ url: '/pages/my/my' }) } } } </script>
使用擴展
如果嫌每次跳轉(zhuǎn)都需要手動開啟loading,那就封裝一下uni的跳轉(zhuǎn)函數(shù)或者攔截一下跳轉(zhuǎn),這種方式并不是很推薦,一般是請求完成了加載頁面,建議封裝在請求里面 —— 大概思路就是,每個頁面最主要的數(shù)據(jù)回來了就可以關閉loading了,又或者是直接寫到頁面里面去等請求執(zhí)行完了關閉,這個得看實際場景。
攔截跳轉(zhuǎn)實現(xiàn)
const switchTabBarInterceptor = () => { uni.addInterceptor('switchTab', { success: function (res) { uni.$emit('pageSwitch', true) }, complete() { uni.$emit('pageSwitch', false) } }) } switchTabBarInterceptor() // 丟main.js 里面就好了..
跳轉(zhuǎn)封裝大概實現(xiàn)
export class Jump { tabBarPage = [] /** * Jump類構(gòu)造函數(shù) * @param {Array} tabBar - 底部tab欄的頁面列表 */ constructor(tabBar) { this.tabBarPage = tabBar } /** * 將傳入的參數(shù)轉(zhuǎn)化成URL參數(shù)字符串 * @param {Object} params - 需要轉(zhuǎn)化的參數(shù)對象 * @returns {string} - 轉(zhuǎn)化后的URL參數(shù)字符串 */ parseParams(params) { return Object.entries(params).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&') } /** * 跳轉(zhuǎn)到某一個頁面 * @param {string} url - 需要跳轉(zhuǎn)的頁面路徑 * @param {Object} params - 需要傳遞的參數(shù)對象 */ push(url, params = {}) { const urlWithParams = Object.keys(params).length ? `${url}?${this.parseParams(params)}` : url const isTabBarPage = this.tabBarPage.some(page => page === url) if (isTabBarPage) { // 還是老樣子,在跳轉(zhuǎn)之前開啟loading uni.$emit('pageSwitch', true) uni.switchTab({ url: urlWithParams }) } else { uni.navigateTo({ url: urlWithParams }) } } }
掛載一手方便使用
/** * @typedef {Object} Vue * @property {Jump} $jump - 跳轉(zhuǎn)類實例 */ Vue.prototype.$jump = new Jump(['/pages/index/index', '/pages/my/my'])
請求關閉loading
在統(tǒng)一管理請求的時候就確定好哪些api是可以關閉loading的
// request.js class Http { ... request(..., closeLoading = false) { // ... uni.request({ complete() { if(closeLoading) uni.$emit('pageSwitch', false) } }) // ... } } // api.js const http = new Http() const fetchOrderList = () => http.request(..., true)
總結(jié)
到此這篇關于uniapp自定義頁面跳轉(zhuǎn)loading的文章就介紹到這了,更多相關uniapp自定義頁面跳轉(zhuǎn)loading內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript中將Object轉(zhuǎn)換為String函數(shù)代碼 (json str)
下面的代碼就是想將Object轉(zhuǎn)換為String函數(shù),需要的朋友可以參考下2012-04-04