uniapp自定義頁面跳轉(zhuǎn)loading的實現(xiàn)代碼
前言
在開發(fā)uniapp項目時,頁面切換的過程中,經(jīng)常需要添加loading效果以提高用戶體驗。uniapp自帶的loading效果無法自定義,所以想著自己實現(xiàn)一個。
實現(xiàn)思路
- 封裝一個全局loading組件,放置插槽和loading效果,使用
v-if或者v-show去進(jìn)行切換。 - 將切換的狀態(tài)isLoading放置在
app.vue中的globalData里面來進(jìn)行狀態(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)到對應(yīng)頁面之后再去手動關(guān)閉即可,或者是在某個請求完成之后去關(guā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>使用擴(kuò)展
如果嫌每次跳轉(zhuǎn)都需要手動開啟loading,那就封裝一下uni的跳轉(zhuǎn)函數(shù)或者攔截一下跳轉(zhuǎn),這種方式并不是很推薦,一般是請求完成了加載頁面,建議封裝在請求里面 —— 大概思路就是,每個頁面最主要的數(shù)據(jù)回來了就可以關(guān)閉loading了,又或者是直接寫到頁面里面去等請求執(zhí)行完了關(guān)閉,這個得看實際場景。
攔截跳轉(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'])請求關(guān)閉loading
在統(tǒng)一管理請求的時候就確定好哪些api是可以關(guān)閉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é)
到此這篇關(guān)于uniapp自定義頁面跳轉(zhuǎn)loading的文章就介紹到這了,更多相關(guān)uniapp自定義頁面跳轉(zhuǎn)loading內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中將Object轉(zhuǎn)換為String函數(shù)代碼 (json str)
下面的代碼就是想將Object轉(zhuǎn)換為String函數(shù),需要的朋友可以參考下2012-04-04
自適應(yīng)高度框架 ----屬個人收藏內(nèi)容
自適應(yīng)高度框架 ----屬個人收藏內(nèi)容...2007-01-01
完美實現(xiàn)js焦點(diǎn)輪播效果(二)(圖片可滾動)
這篇文章主要為大家詳細(xì)介紹了完美實現(xiàn)js焦點(diǎn)輪播效果的相關(guān)代碼,采用輔助圖片實現(xiàn)圖片無限滾動,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03

