1.首先在项目根目录创建vue.config.js文件代码如下;
module.exports = {
chainWebpack: config => {
config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {
const compile = options.compiler.compile
options.compiler.compile = (template, ...args) => {
if(!args[0].resourcePath){
return compile(template, ...args)
}
if ((args[0].resourcePath.match(/^pages/) && !args[0].resourcePath.match(/^pages\/user\/index\/index/)) || args[0].resourcePath.match(/^pageA/) || args[0].resourcePath.match(/^pagesLive/)) {
template = template.replace(/[\s\S]+?<[\d\D]+?>/, _ => `${_}
<new-request-loading></new-request-loading>
`)
}
return compile(template, ...args)
}
return options
})
}
}
2.main.js添加这段代码替换uniapp全局loading方法并且全局挂载组件文章来源:https://www.toymoban.com/news/detail-737892.html
// #ifdef MP-WEIXIN
uni.showLoading = function(){
store.commit("changeLoading",true)
// 注销uniapp showLoading方法
}
uni.hideLoading = function(){
store.commit("changeLoading",false)
}
// #endif
//请求加载组件
import newRequestLoading from './components/ccloading.vue';
//组件挂载到全局,方便每个页面使用
Vue.component('new-request-loading', newRequestLoading);
3.添加loading组件通过vuex控制组件loading状态文章来源地址https://www.toymoban.com/news/detail-737892.html
<template>
<view class="request-loading-view" v-if="loading">
<view class="loading-view">
<image class="loading-img" src="../static/loading.gif" mode=""></image>
</view>
</view>
</template>
<script>
import {
mapGetters
} from 'vuex'
export default {
data() {
return {};
},
computed: {
...mapGetters(['loading'])
},
};
</script>
<style scoped>
.request-loading-view {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 999999;
background-color: rgba(0, 0, 0, 0.001);
display: flex;
justify-content: center;
align-items: center;
}
.loading-view {
width: 160upx;
height: 160upx;
/* background-color: rgba(0, 0, 0, 0.6); */
border-radius: 20upx;
display: flex;
justify-content: center;
align-items: center;
}
/* 动画样式 */
.loading {
border: 10upx solid rgba(0, 0, 0, 0.01);
border-radius: 50%;
border-top: 10upx solid #666666;
border-right: 10upx solid #666666;
border-bottom: 10upx solid #666666;
width: 60upx;
height: 60upx;
-webkit-animation: spin 1.4s linear infinite;
animation: spin 1.4s linear infinite;
}
.loading-img {
width: 60upx;
height: 60upx;
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
到了这里,关于uniapp自定义全局loading组件(目前只适配微信小程序)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!