自定义组件 v-loading

通过指令方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<template>
  <div class="loading-container">
    <icon-sync class="icon" spin />
  </div>
</template>
 
<script lang="ts" setup>
  import { IconSync } from '@arco-design/web-vue/es/icon'
</script>
 
<style lang="less" scoped>
  .loading-container {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.6);
 
    .icon {
      color: #409eff;
      font-size: 32px;
    }
  }
</style>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { DirectiveBinding, createApp } from 'vue'
import Loading from './index.vue'
 
const toggle = (value: boolean, el: any) => {
  if (value) {
    el.appendChild(el.instance.$el)
  } else {
    const container = el.querySelector('.loading-container')
    if (container) {
      el.removeChild(container)
    }
  }
}
 
export default {
  mounted(el: any, binding: DirectiveBinding) {
    const app = createApp(Loading)
    const instance = app.mount(document.createElement('div'))
    el.instance = instance
    el.style.position = 'relative'
    toggle(binding.value, el)
 
  },
  updated(el: HTMLElement, binding: DirectiveBinding) {
    console.log(binding.value)
    toggle(binding.value, el)
  }
}

组件包裹的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<template>
  <a-spin :loading="loading" style="display: inherit">
    <slot></slot>
  </a-spin>
</template>
 
<script lang="tsx" setup>
  import { computed } from 'vue'
  import { loadingStore } from '@/store/modules/loading'
 
  const storeLoading = loadingStore() as any
  const props = defineProps({
    loadingName: {
      type: String
    }
  })
 
  const loading = computed(() => {
    const name = props.loadingName || 'loading'
    return storeLoading.$state[name]
  })
</script>

  

 

posted @   偷甜瓜香喷喷  阅读(115)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示