当前位置:   article > 正文

uniapp自定义全局loading组件(目前只适配微信小程序)_uniapp全局loading

uniapp全局loading

1.首先在项目根目录创建vue.config.js文件代码如下;

  1. module.exports = {
  2. chainWebpack: config => {
  3. config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {
  4. const compile = options.compiler.compile
  5. options.compiler.compile = (template, ...args) => {
  6. if(!args[0].resourcePath){
  7. return compile(template, ...args)
  8. }
  9. if ((args[0].resourcePath.match(/^pages/) && !args[0].resourcePath.match(/^pages\/user\/index\/index/)) || args[0].resourcePath.match(/^pageA/) || args[0].resourcePath.match(/^pagesLive/)) {
  10. template = template.replace(/[\s\S]+?<[\d\D]+?>/, _ => `${_}
  11. <new-request-loading></new-request-loading>
  12. `)
  13. }
  14. return compile(template, ...args)
  15. }
  16. return options
  17. })
  18. }
  19. }

2.main.js添加这段代码替换uniapp全局loading方法并且全局挂载组件

  1. // #ifdef MP-WEIXIN
  2. uni.showLoading = function(){
  3. store.commit("changeLoading",true)
  4. // 注销uniapp showLoading方法
  5. }
  6. uni.hideLoading = function(){
  7. store.commit("changeLoading",false)
  8. }
  9. // #endif
  10. //请求加载组件
  11. import newRequestLoading from './components/ccloading.vue';
  12. //组件挂载到全局,方便每个页面使用
  13. Vue.component('new-request-loading', newRequestLoading);

3.添加loading组件通过vuex控制组件loading状态

  1. <template>
  2. <view class="request-loading-view" v-if="loading">
  3. <view class="loading-view">
  4. <image class="loading-img" src="../static/loading.gif" mode=""></image>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. import {
  10. mapGetters
  11. } from 'vuex'
  12. export default {
  13. data() {
  14. return {};
  15. },
  16. computed: {
  17. ...mapGetters(['loading'])
  18. },
  19. };
  20. </script>
  21. <style scoped>
  22. .request-loading-view {
  23. position: fixed;
  24. top: 0;
  25. left: 0;
  26. width: 100vw;
  27. height: 100vh;
  28. z-index: 999999;
  29. background-color: rgba(0, 0, 0, 0.001);
  30. display: flex;
  31. justify-content: center;
  32. align-items: center;
  33. }
  34. .loading-view {
  35. width: 160upx;
  36. height: 160upx;
  37. /* background-color: rgba(0, 0, 0, 0.6); */
  38. border-radius: 20upx;
  39. display: flex;
  40. justify-content: center;
  41. align-items: center;
  42. }
  43. /* 动画样式 */
  44. .loading {
  45. border: 10upx solid rgba(0, 0, 0, 0.01);
  46. border-radius: 50%;
  47. border-top: 10upx solid #666666;
  48. border-right: 10upx solid #666666;
  49. border-bottom: 10upx solid #666666;
  50. width: 60upx;
  51. height: 60upx;
  52. -webkit-animation: spin 1.4s linear infinite;
  53. animation: spin 1.4s linear infinite;
  54. }
  55. .loading-img {
  56. width: 60upx;
  57. height: 60upx;
  58. }
  59. @-webkit-keyframes spin {
  60. 0% {
  61. -webkit-transform: rotate(0deg);
  62. }
  63. 100% {
  64. -webkit-transform: rotate(360deg);
  65. }
  66. }
  67. @keyframes spin {
  68. 0% {
  69. transform: rotate(0deg);
  70. }
  71. 100% {
  72. transform: rotate(360deg);
  73. }
  74. }
  75. </style>

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

闽ICP备14008679号