选中孩子后,家长Vuetify VMenu无法关闭

时间:2018-12-28 14:32:38

标签: vue.js navigation vuetify.js

我的应用程序通过for循环动态生成其主导航,该循环用于生成嵌套的vmenu和vlist。它会正确生成。问题是当用户单击嵌套菜单中的链接时,父菜单保持打开状态。我已经在主菜单和嵌套菜单上尝试了单击关闭和内容单击关闭,但这是行不通的。

我的codepen有一个简化的示例。如果选择MENU 1,则将鼠标悬停在Subitem 1-1上并单击Subitem 1-1-1,该即时菜单将关闭,但主菜单保持打开状态。

My codepen

<div id="app">
  <v-app id="inspire">
    <v-container>
      <!-- top level menu -->
      <v-menu
        v-for="(menu,index) in menus"
        :key="`menu-${index}`"
        offset-y
        close-on-click
        @click="route()"
      >
        <v-btn
          flat
          primary
          slot="activator"
        >{{ menu.title }}</v-btn>
        <!-- each item on a menu  ..  is a menuitem -->
        <v-list dense>
          <v-list-tile
            v-for="(menuitem,index) in menu.items"
            :key="`menuitem-${index}`"
            @click="route()"
          >
            <!-- or a popout of submenu items -->
            <v-list-tile-content>
              <v-menu
                offset-x
                open-on-hover
                close-on-click
              >
                <v-list-tile
                  slot="activator"
                  @click="route()"
                >
                  <v-list-tile-title>{{ menuitem.title }}</v-list-tile-title>
                  <v-list-tile-action
                    class="justify-end"
                    v-if="menuitem.items"
                  >
                    <v-icon primary>arrow_right</v-icon>
                  </v-list-tile-action>
                </v-list-tile>
                <v-list
                  dense
                  v-if="menuitem.items"
                >
                  <v-list-tile
                    v-for="(menusubitem,index) in menuitem.items"
                    :key="`menusubitem-${index}`"
                    @click="route()"
                  >
                    <v-list-tile-title>{{ menusubitem.title }}</v-list-tile-title>
                  </v-list-tile>
                </v-list>
              </v-menu>
            </v-list-tile-content>
          </v-list-tile>
        </v-list>
      </v-menu>
    </v-container>
  </v-app>
</div>

以及相关脚本。

new Vue({
  el: '#app',
  data: () => ({    
    menus: [
      {
        title: 'MENU 1',
        items: [
          { title: 'Subitem 1-1',
            items: [

              { title: 'Subitem 1-1-1' },
              { title: 'Subitem 1-1-2' }
            ]
          },
          { title: 'Subitem 1-2' },
          { title: 'Subitem 1-3' }
        ]  
      },
      {
        title: 'MENU 2',
      },
      {
        title: 'MENU 3',
        items: [
          { title: 'Subitem 3-1' },
          { title: 'Subitem 3-2' },
        ]  
      }]
  }),
  methods: {
    route() {
      console.log("replace with router")
    } 
  }
})

1 个答案:

答案 0 :(得分:0)

一种选择是通过isActive属性显式访问和关闭父菜单:

route(parentMenuIndex) {
  if (arguments.length) {
      const parentMenu = this.$refs.menuRef[parentMenuIndex];
      parentMenu.isActive = false;
  }
}

其中this.$refs.menuRef用于通过ref attribute访问 parent 菜单组件:

<v-menu
      v-for="(menu,index) in menus"
      :key="`menu-${index}`"
      offset-y
      close-on-click
      close-on-content-click
      ref="menuRef"
>
...
</v-menu>

这里是updated CodePen