简体   繁体   English

启用使用 routerlink 作为子 vue 3 单击父标签

[英]enable clickling on parent tag with routerlink as child vue 3

I have a RouterLink used inside a li tag and which basically makes a simple dropdown menu, When options are present on the dropdown menu I can click on the text to go to the next page but not click anywhere else inside the box, instead I want to be able to click on the whole "bar" which is the li tag to be able to go to the page我有一个在li标签内使用的RouterLink ,它基本上是一个简单的下拉菜单,当下拉菜单上存在选项时,我可以单击文本到 go 到下一页,但不能单击框内的其他任何地方,而是我想要能够单击整个“栏”,即li标签能够 go 到页面

<ul>
  <li v-for="product in products">
     <RouterLink :to="/product"> {{product}} </RouterLink>
  </li>
</ul>

this creates a dropdown like this:这会创建一个像这样的下拉列表:

|product name | |产品名称 |

|product 2 name | |产品2名称|

Now clicking on only text works but I want to be able to click inside the box as well.现在仅单击文本有效,但我也希望能够在框内单击。 How do you solve that.你怎么解决这个问题。 Another question is how do I close the dropdown when it is clicked anywhere else.另一个问题是如何在其他任何地方单击下拉菜单时关闭它。

The intuitive solution is to add click event handler to the li tag and use $router.push('/product') :直观的解决方案是将点击事件处理程序添加到li标签并使用$router.push('/product')

<ul>
  <li v-for="product in products" @click="$router.push('/product')">
       {{product}}
  </li>
</ul>

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

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