美文网首页
获取当前节点的后代组件数量

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

作者: 苍老师的眼泪 | 来源:发表于2022-03-16 13:42 被阅读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>

相关文章

网友评论

      本文标题:获取当前节点的后代组件数量

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