js里面有递归算法,同时,咱们也能够利用props来实现vue模板的递归调用,可是前提是组件拥有 name 属性html
父组件:slotDemo.vue:vue
<template> <div> <!-----递归组件-----> <ul> <simple3 :tree="item" v-for="item in tree"></simple3> </ul> </div> </template> <style lang="stylus" rel="stylesheet/stylus"> li padding-left 30px </style> <script> import simple3 from "./simple/simple3.vue"; export default{ data(){ return { tree: [{ label: "一级菜单", test:1, children: [{ label: "二级菜单", test:2, children: [{ label: "三级菜单", test:3 }] }] }] } }, components: { simple3 } } </script>
子组件:simple3.vue算法
<template> <li> <a>{{tree.label}}</a> <simple3 v-if="tree.children" :tree="item" v-for="item in tree.children" :class="item.test==2?'test2':'test3'"></simple3> </li> </template> <style rel="stylesheet/stylus" lang="stylus"> .test2 list-style disc .test3 list-style decimal </style> <script> export default{ name: "simple3", props: ["tree"] } </script>
上面是一个子组件,定义了 name 为 simple03,而后在模板中调用自身,结合 v-for 实现递归component
为了防止出现死循环,在调用自身的时候,加入了 v-if 做为断定条件htm
父组件中调用的时候,须要经过 props 传入一个 tree;blog
为了对每一级菜单有所区分,我对tree里面的每个子集合里面加了一个test字段来区分是哪一级的菜单而后对其不一样的样式进行处理递归
最后的效果:ip