获取当前节点的后代组件数量

2022-03-16  本文已影响0人  苍老师的眼泪

get_houdai.js

export default function get_houdai(node) {
    
    let total_houdai = 1 // 默认值1 是它自己
    
    for (const houdai of node.$children)
        total_houdai += get_houdai(houdai)
        
    return total_houdai 
        
}

main.js

import get_houdai from '@/get_houdai'

Vue.prototype.$get_houdai = get_houdai

组件(点击显示后代个数)

<template>
    <div id="zxcv" @click.self="show_subs">

    </div>
</template>

<script>
export default {
  methods: {
    show_subs() {
            console.log(this.$get_houdai(this))

    },
  },
}
</script>

<style lang="less">
    #zxcv {
        width: 100px;
        height: 100px;
        background-color: coral;
    }
</style>
上一篇 下一篇

猜你喜欢

热点阅读