日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第十三课时:递归组件的使用

發布時間:2024/4/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第十三课时:递归组件的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遞歸組件的使用

1. 封裝簡單的Menu組件

一個子組件就相當于一個父組件的html標簽
父組件: menu-page.vue
子組件: a-menu.vue、a-menu-item.vue、a-submenu.vue
還有個暴露子組件的index.js

先上一張最后渲染的結果:

menu-page.vue

<template><div class="menu-box"><a-menu><a-menu-item>111</a-menu-item><a-menu-item>222</a-menu-item><a-submenu><div slot="title">333</div><a-menu-item>333-11</a-menu-item><a-submenu><div slot="title">333-22</div><a-menu-item>333-22-1</a-menu-item><a-menu-item>333-22-2</a-menu-item></a-submenu></a-submenu></a-menu></div> </template> <script> import menuComponents from '_c/menu' const { AMenu, AMenuItem, ASubmenu } = menuComponentsexport default {name: 'menu_page',components: {AMenu,AMenuItem,ASubmenu} } </script><style lang="less"> .menu-box{width: 300px;height: 400px; } </style>

a-menu.vue

<template><div class="a-menu"><slot></slot></div> </template><script> export default {name: 'AMenu' } </script><style lang="less"> .a-menu{& *{list-style: none;}ul{padding: 0;margin: 0;} } </style>

a-menu-item.vue

<template><div><li class="a-menu-item"><slot></slot></li></div> </template><script> export default {name: 'AMenuItem' } </script><style lang="less"> .a-menu-item{background: red; } </style>

a-submenu.vue

<template><ul class="a-submenu"><div class="a-submenu-title" @click="handClick"><slot name="title"></slot><span class="shrink-icon" :style="{ transform: `rotateZ(${showChild ? 0 : 180}deg)`}">^</span></div><div v-show="showChild" class="a-submenu-child-box"><slot></slot></div></ul> </template><script> export default {name: 'ASubmenu',data () {return {showChild: false}},methods: {handClick () {this.showChild = !this.showChild}} } </script><style lang="less"> .a-submenu{background: gray;color: #fff;&-title{position: relative;.shrink-icon{position: absolute;top: 2px;right: 10px;}}&-child-box{overflow: hidden;padding-left: 20px;}li{background: gray;} } </style>

index.js

import AMenu from './a-menu.vue' import AMenuItem from './a-menu-item.vue' import ASubmenu from './a-submenu.vue'export default{AMenu,AMenuItem,ASubmenu }

2.遞歸組件

子組件不斷調用自身(需要組件有name屬性),接上面組件的例子,修改menu-page.vue,新增re-subname.vue

最后渲染的效果圖

menu-page.vue

<template><div class="menu-box"><a-menu><template v-for="(amenu, index) in list"><a-menu-item :key="index" v-if="!amenu.children">{{amenu.title}}</a-menu-item><re-submenu v-else :menu="amenu" :index="index" :key="index"></re-submenu></template></a-menu></div> </template> <script> import menuComponents from '_c/menu' import reSubmenu from './re-submenu.vue' const { AMenu, AMenuItem, ASubmenu } = menuComponentsexport default {name: 'menu_page',data () {return {list: [{title: '1111'},{title: '2222'},{title: '3333',children: [{title: '3333-1'},{title: '3333-2',children: [{title: '3333-2-1'},{title: '3333-2-2'},{title: '3333-2-3',children: [{title: '3333-2-3-1'},{title: '3333-2-3-2'}]}]}]}]}},components: {AMenu,AMenuItem,ASubmenu,reSubmenu} } </script><style lang="less"> .menu-box{width: 300px;height: 400px; } </style>

re-subname.vue

<template><a-submenu><div slot="title">{{ menu.title }}</div><template v-for="(amenu, jndex) in menu.children"><a-menu-item :key="jndex" v-if="!amenu.children">{{amenu.title}}</a-menu-item><re-submenu v-else :menu="amenu" :index="`${index}_${jndex}`" :key="`${index}_${jndex}`"></re-submenu></template></a-submenu> </template><script> import menuComponents from '_c/menu' const { AMenuItem, ASubmenu } = menuComponentsexport default {name: 'reSubmenu',components: {AMenuItem,ASubmenu},props: {menu: {type: Object,default: function () {return {}}},index: {type: Number}} } </script>

總結

以上是生活随笔為你收集整理的第十三课时:递归组件的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。