To be clear what I want to do...
When Menu 1
is Clicked, its ul
will be shown and likewise if Menu 1
is shown ul
, and Menu 1
is Clicked, ul
is not displayed.
same applies to Menu 2
Can you show me as an example how can I do what I want to do?
Menu 1
Click Menu 1
ul show or ul show close
<ul>
<li v-for="(item,index) in menuData" :key="index">
<router-link :to="item.path" exact @click.stop="showMenu(index)">{{ item.name }}</router-link>
<ul v-if="item.children" v-show="showCollapsed">
<li v-for="(childItem,ChildIndex) in item.children" :key="ChildIndex">
<router-link :to="childItem.path">{{ childItem.name }}</router-link>
</li>
</ul>
</li>
</ul>
<script>
export default {
name: "App",
data() {
return {
openshow: false,
selected: null,
menuData: [{name: "Home", path: "/",}, {name: "About", path: "/about",}, {name: "Menu 1", path: "#", children: [{name: "Menu 1.1", path: "/menu/1/1",}, {name: "Menu 1.2", path: "/menu/1/2",}, {name: "Menu 1.3", path: "/menu/1/3",},],}, {name: "Menu 2", path: "#", children: [{name: "Menu 2.1", path: "/menu/2/1",}, {name: "Menu 2.2", path: "/menu/2/2",}, {name: "Menu 2.3", path: "/menu/2/3",},],}, {name: "Gallery", path: "/gallery",}, {name: "Contact", path: "/contact",},],
}
},
methods: {
showMenu(index) {
this.selected = index;
},
showCollapsed() {
}
}
}
</script>
CodePudding user response:
Just use showMenu
method with passed index and then check what ul
to show passing index to showCollapsed
:
const app = Vue.createApp({
data() {
return {
openshow: false,
selected: null,
menuData: [{name: "Home", path: "/",}, {name: "About", path: "/about",}, {name: "Menu 1", path: "#", children: [{name: "Menu 1.1", path: "/menu/1/1",}, {name: "Menu 1.2", path: "/menu/1/2",}, {name: "Menu 1.3", path: "/menu/1/3",},],}, {name: "Menu 2", path: "#", children: [{name: "Menu 2.1", path: "/menu/2/1",}, {name: "Menu 2.2", path: "/menu/2/2",}, {name: "Menu 2.3", path: "/menu/2/3",},],}, {name: "Gallery", path: "/gallery",}, {name: "Contact", path: "/contact",},],
}
},
methods: {
showMenu(index) {
this.selected === index ? this.selected = null : this.selected = index;
},
showCollapsed(idx) {
return idx === this.selected || false
}
}
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
<ul>
<li v-for="(item,index) in menuData" :key="index" @click="showMenu(index)">
<router-link :to="item.path" exact>{{ item.name }}</router-link>
<ul v-if="item.children" v-show="showCollapsed(index)">
<li v-for="(childItem,ChildIndex) in item.children" :key="ChildIndex">
<router-link :to="childItem.path">{{ childItem.name }}</router-link>
</li>
</ul>
</li>
</ul>
</div>