美文网首页
VUE 2.x中全局组件的封装(二)

VUE 2.x中全局组件的封装(二)

作者: _皓月__ | 来源:发表于2021-04-20 11:43 被阅读0次

    1.loading组件

    <template>
      <transition name="fade">
        <section>
          <div class="loading">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <p>{{ title }}</p>
          </div>
        </section>
      </transition>
    </template>
    <script>
    export default {
      props: {
        title: {
          type: String,
          default: '正在载入...',
        },
      },
    }
    </script>
    <style scoped lang="css">
    .fade-enter-active,
    .fade-leave-active {
      transition: opacity 0.5s;
    }
    .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
      opacity: 0;
    }
    section {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.3);
      z-index: 999;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .loading {
      width: 100%;
      text-align: center;
      /* flex-direction: column; */
    }
    .loading span {
      display: inline-block;
      width: 8px;
      height: 100%;
      border-radius: 4px;
      background: lightgreen;
      animation: load 1s ease infinite;
      margin-left: 5px;
    }
    @keyframes load {
      0%,
      100% {
        height: 40px;
        background: lightgreen;
      }
      50% {
        height: 70px;
        margin: -15px 0;
        background: lightblue;
      }
    }
    .loading span:nth-child(2) {
      animation-delay: 0.2s;
    }
    .loading span:nth-child(3) {
      animation-delay: 0.4s;
    }
    .loading span:nth-child(4) {
      animation-delay: 0.6s;
    }
    .loading span:nth-child(5) {
      animation-delay: 0.8s;
    }
    </style>
    

    2.在loading.vue同级下创建一个loading.js

    import loading from './loading.vue';
    
    const Loading = {
        install:function(Vue) {
            Vue.component('Loading', loading)
        }
    }
    
    export default Loading;
    

    3.在main.js注册

    import Vue from 'vue'
    import loading from './components/loading'
    Vue.use(loading)
    

    4.在组件中使用封装好的loading组件

    <loading v-if="hide"></loading>
    

    原文链接:https://segmentfault.com/a/1190000020491447

    相关文章

      网友评论

          本文标题:VUE 2.x中全局组件的封装(二)

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