Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs: Is there a way to access router.go() inside a component?

Tags:

I want to call router.go inside a component

currently i have set window.router = router // so the code works

index.js

const App = Vue.extend(require('./views/app.vue')) router.start(App, '#app') window.router = router // I don't want to set this global variable 

navbar.vue

methods: {         search () {             window.router.go({                 name: 'search',                 query: { q: this.query }             })         } } 

what i am looking for is something like, this.$router.go

navbar.vue

methods: {         search () {             this.$router.go({                 name: 'search',                 query: { q: this.query }             })         }     } 

Thanks and really appreciate some help

like image 433
Anish Dcruz Avatar asked Oct 08 '15 16:10

Anish Dcruz


People also ask

How do I access my vue router in component?

To have the Vue-Router routes be rendered you will need to pass the <router-view> tag inside a Vue component. You could also access the routes from an <a> tag, but this will trigger a page re-render, to avoid this behavior you could use router-link with the to property instead of a href .

How can I get previous route name in Vuejs?

As an example you could use beforeRouteEnter , an in-component navigation guard, to get the previous route and store it in your data .. ... data() { return { ... prevRoute: null } }, beforeRouteEnter(to, from, next) { next(vm => { vm.

How do I push a route at vue?

With Vue Router, you can use its router. push() function to programmatically navigate between routes on your site. You can either call push() with a string path, or with an object containing either the path or name of the route.

Can vue router open a link in a new tab?

to open a Vue Router link in a new tab, we can call the $router. resolve method to get the full URL from the route name, path, parameter and query values. Then we can use window. open to open the URL in a new tab.


1 Answers

For vue-router 2.x

this.$router.push('path') 

For vue-router 1.x

this.$route.router.go('path') 
like image 131
Bill Criswell Avatar answered Sep 21 '22 13:09

Bill Criswell