search

Home  >  Q&A  >  body text

javascript - How to get router in vue single file component

<template>
    <p>
        a.vue
        <router-link to="/a/a1">a1</router-link>
        <router-link to="/a/a2">a2</router-link>
        <br>
        <button @click="gta1()">a1</button>
        <router-view></router-view>
    </p>
</template>
<script>

    export default{
        methods:{
            gta1(){
                router.push({path:'a'}) //这里报错了, “router is not defined”
            }
        }
    }
</script>

I would like to ask, how can I obtain the router and implement programmatic navigation?

阿神阿神2701 days ago894

reply all(3)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 10:50:49

    this.$router

    reply
    0
  • 某草草

    某草草2017-07-05 10:50:49

    See component injection for details

    $router: router instance.
    $route: currently activated routing information object.

    reply
    0
  • 天蓬老师

    天蓬老师2017-07-05 10:50:49

     this.$router.push({path:'a'})

    reply
    0
  • Cancelreply