Vue3 component encapsulation, taking the encapsulation of Loading component as an example

The meaning of component packaging

提示:本文技术栈为vue3+ts+antdv

  • For better reuse
  • Decoupling project code
  • Encapsulate reusable code
  • Better coupling of code logic (when a component is reused in multiple places, you only need to modify the component, instead of modifying every place)
  • Improve development efficiency

The loading effect is similar to the following:
insert image description here

Loading component encapsulation (subcomponent)

<template>
  <a-spin size="large" class="postion" :spinning="data" />
</template>

<script lang="ts" setup>
const data = ref<boolean>(true)
const props = defineProps<{
    
    
  spinning: boolean
}>()
const {
    
     spinning } = toRefs(props)
watch(
  () => spinning.value,
  () => {
    
    
    data.value = spinning.value
  }
)
</script>

<style lang="less" scoped>
.postion {
    
    
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>

Component usage (parent component)

 <loading :spinning="loading" />
import Loading from '@/components/loading/index.vue' //引入
const loading = ref<boolean>(true) //定义变量
//显示加载动画
loading.value = true
//关闭加载动画
loading.value = false

Finally, the encapsulation of a simple component is completed. For the encapsulation of complex components, the author will update it later...

Guess you like

Origin blog.csdn.net/m0_46627407/article/details/125641852