美文网首页vue组件
vue中自己封装loading组件

vue中自己封装loading组件

作者: 小码农_影 | 来源:发表于2020-10-22 10:20 被阅读0次

    loading.vue

      <template>
          <div v-if="show">
            <div class="mask_loading"><img src="@/assets/phone/loading.gif" alt="" class="loading"></div>
        </div>
    </template>
    
    <script>
      export default {
        name: "loading",
        data(){
            return{
            }
    
        },
        props: {
            show: Boolean
        },
    }
    </script>
    <style scoped lang="scss">
    .loading{
        width: 100px;
        height: 80px;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -50px;
        margin-top: -40px;
        z-index: 13;
    }
    .mask_loading{
      width: 100%;
      height: 100%;
      background:rgba(255,255,255,0);
      position: fixed;
      top: 0;
      z-index: 99999;
    }
    </style>
    

    loading.js

    import Vue from 'vue'
    import loadingComponent from '@/app-client/component/loading.vue'
    
    const LoadingConstructor = Vue.extend(loadingComponent)
    
    const instance = new LoadingConstructor({
        el: document.createElement('div')
    })
    
    instance.show = false // 默认隐藏
    const loading = {
     show() { // 显示方法
        instance.show = true
        document.body.appendChild(instance.$el)
      },
      hide() { // 隐藏方法
          instance.show = false
      }
    }
    
    export default {
        install() {
        if (!Vue.$loading) {
            Vue.$loading = loading
        }
        Vue.mixin({
            created() {
                this.$loading = Vue.$loading
            }
        })
    }
    }
    

    使用

    main.js中引入loading组件,然后就可以在任意一个组件中调用loading方法
    import loading from '@/app-client/component/loading.js' // 引入loading
    
    在组件中使用
    this.$loading.show()
    this.$loading.hide()
    

    相关文章

      网友评论

        本文标题:vue中自己封装loading组件

        本文链接:https://www.haomeiwen.com/subject/hpwrmktx.html