Enclosing a router-link tag in a button in vuejs

2019-03-14 08:27发布

Can I wrap or enclose a router-link tag in a button tag?

When I press the button, I want it to route me to the desired page.

5条回答
一夜七次
2楼-- · 2019-03-14 08:39

You can use tag prop.

<router-link to="/foo" tag="button">foo</router-link>
查看更多
The star\"
3楼-- · 2019-03-14 08:43

I'm working on Vue CLI 2 and this one has worked for me!

<router-link to="/about-creator">
<button>About Creator</button>
</router-link>
查看更多
Fickle 薄情
4楼-- · 2019-03-14 08:53

Now days (VueJS >= 2.x) you would do:

<router-link tag="button" class="myClass" id="button" :to="place.to.go">Go!</router-link>
查看更多
你好瞎i
5楼-- · 2019-03-14 08:56

@choasia's answer is correct.

Alternatively, you could wrap a button tag in a router-link tag like so:

<router-link :to="{name: 'myRoute'}">
  <button id="myButton" class="foo bar">Go!</button>
</router-link>

This is not so clean because your button will be inside a link element (<a>). However, the advantage is that you have a full control on your button, which may be necessary if you work with a front-end framework like Bootstrap.

I have never used this technique on buttons, to be honest. But I did this on divs quite often...

查看更多
Animai°情兽
6楼-- · 2019-03-14 09:00

While the answers on here are all good, none seem to be the simplest solution. After some quick research, it seems that the real easiest way to make a button use vue-router is with the router.push call. This can be used within a .vue template like this:

<button @click="$router.push('about')">Click to Navigate</button>

Super simple and clean. I hope someone else finds this useful!

Source: https://router.vuejs.org/guide/essentials/navigation.html

查看更多
登录 后发表回答