简体   繁体   English

单击路由器链接时显示或隐藏画布外菜单

[英]Show or hide offcanvas menu when router link is clicked

I have a vue electron app where the main menu template is loaded inside the App.vue file.我有一个 vue electron 应用程序,其中主菜单模板加载到App.vue文件中。 I want to hide it when a <router-link> is clicked.单击<router-link>时,我想隐藏它。 It's an offcanvas menu so only the hamburger button is always displayed in all the components and the content will be showed only when the button is clicked.这是一个 offcanvas 菜单,因此只有汉堡包按钮始终显示在所有组件中,并且只有在单击按钮时才会显示内容。 Is there any valid solution to manage this?有没有有效的解决方案来管理这个?

This is a sample of the menu markup that is inside the main view loaded inside the App.vue file这是在App.vue文件中加载的主视图中的菜单标记示例

<template>
<div>
 <nav v-if="isVisible">
  <button @click.prevent="toggleMenu()"><i class="fas fa-bars"></i></button>
  <li><router-link to="/"></router-link></li>
  <li><router-link to="/link-one"></router-link></li>
 </nav>
 <router-view></router-view>
</div> 
</template>

<script>
export {
 methods: {
  toggleMenu() {
   this.isVisible = !this.isVisible;
  }
 }
}
</script>

At the moment the offcanvas content will remain on screen also after the router view will change.目前,在路由器视图更改后,offcanvas 内容也将保留在屏幕上。

use click.native?使用 click.native?
Did a quick search on stackoverflow, looks like click on router-link will trigger click.native在 stackoverflow 上快速搜索了一下,看起来点击 router-link 会触发 click.native
https://stackoverflow.com/a/52230500/13703967 https://stackoverflow.com/a/52230500/13703967

<template>
<div>
 <nav v-if="isVisible">
  <button @click.prevent="toggleMenu()"><i class="fas fa-bars"></i></button>
  <li><router-link to="/" @click.native="toggleMenu"></router-link></li>
  <li><router-link to="/link-one" @click.native="toggleMenu"></router-link></li>
 </nav>
 <router-view></router-view>
</div> 
</template>

<script>
export {
 methods: {
  toggleMenu() {
   this.isVisible = !this.isVisible;
  }
 }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM