2018-10-24 11:20:14 +00:00
< cn >
#### 单文件递归菜单
使用单文件方式递归生成菜单。
2019-02-23 04:11:54 +00:00
因组件内部会动态更改`a-sub-menu`的属性,如果拆分成单文件,无法将属性挂载到`a-sub-menu`上,你需要自行声明属性并挂载。为了方便,避免属性的声明,我们推荐使用函数式组件。
2018-10-24 11:20:14 +00:00
< / cn >
< us >
#### Single file recursive menu
Use the single file method to recursively generate menus.
2019-02-23 04:11:54 +00:00
The properties of `a-sub-menu` are dynamically changed inside the component. If you split the file into a single file and you cannot mount the `props` to `a-sub-menu` , you need to declare the `props` and mount it yourself. For convenience, to avoid the declaration of attributes, we recommend using functional components.
2018-10-24 11:20:14 +00:00
< / us >
2019-10-09 10:32:23 +00:00
```tpl
2018-10-24 11:20:14 +00:00
< template >
< div style = "width: 256px" >
< a-button type = "primary" @click =" toggleCollapsed " style = "margin-bottom: 16px" >
< a-icon :type = "collapsed ? 'menu-unfold' : 'menu-fold'" / >
< / a-button >
< a-menu
:defaultSelectedKeys="['1']"
2018-12-13 01:40:41 +00:00
:defaultOpenKeys="['2']"
2018-10-24 11:20:14 +00:00
mode="inline"
theme="dark"
:inlineCollapsed="collapsed"
>
< template v-for = "item in list" >
< a-menu-item v-if = "!item.children" :key = "item.key" >
< a-icon type = "pie-chart" / >
< span > {{item.title}}< / span >
< / a-menu-item >
2019-09-28 12:45:07 +00:00
< sub-menu v-else :menu-info = "item" :key = "item.key" / >
2018-10-24 11:20:14 +00:00
< / template >
< / a-menu >
< / div >
< / template >
< script >
2019-09-28 12:45:07 +00:00
/*
* recommend SubMenu.vue https://github.com/vueComponent/ant-design-vue/blob/master/components/menu/demo/SubMenu.vue
* SubMenu1.vue https://github.com/vueComponent/ant-design-vue/blob/master/components/menu/demo/SubMenu1.vue
* */
import SubMenu from './SubMenu';
export default {
components: {
'sub-menu': SubMenu,
2018-10-24 11:20:14 +00:00
},
2019-09-28 12:45:07 +00:00
data() {
return {
collapsed: false,
list: [
{
key: '1',
title: 'Option 1',
},
{
key: '2',
title: 'Navigation 2',
children: [
{
key: '2.1',
title: 'Navigation 3',
children: [{ key: '2.1.1', title: 'Option 2.1.1' }],
},
],
},
],
};
},
methods: {
toggleCollapsed() {
this.collapsed = !this.collapsed;
},
},
};
2018-10-24 11:20:14 +00:00
< / script >
```