Enclosing a router-link tag in a button in vuejs

前端 未结 10 697
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 15:34

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.

相关标签:
10条回答
  • 2020-12-23 16:28

    @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...

    0 讨论(0)
  • 2020-12-23 16:28

    Routing methods using method as well routing 1. Router link

    <router-link to="/login">Login </router-link>

    1. When click on button that type call another route.
    <template>
        <input type="submit" value="Login" @click="onLogIn" class="btn float-right btn-primary">
        </template>
    
    <script>
    export default {
        name:'auth',
        data() {
            return {}
        },
        methods: {
            onLogIn() {
                this.$router.replace('/dashboard');
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-23 16:35

    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

    0 讨论(0)
  • 2020-12-23 16:37

    An example using bootstrap-vue and creating a link with the id inside a table row:

    <b-table :fields="fields" :items="items">
        <template v-slot:cell(action)="data">
            <router-link :to="`/rounds/${data.item.id}`" v-slot="{ href, route, navigate}">
                <b-button :href="href" @click="navigate" color="primary" style="text">{{ data.item.id }}</b-button>
            </router-link>
        </template>
    </b-table>
    

    Where fields and items are the columns names and the table content, respectively.

    0 讨论(0)
提交回复
热议问题