2015-10-08 34 views
15

我想打电话给router.go一个组件内vuejs:有没有办法访问组件内的router.go()?

目前我有设置window.router = router //所以代码工作

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 } 
      }) 
     } 
} 

什么我期待的是类似的,this.$router.go

navbar.vue

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

谢谢,真的很感激一些帮助

回答

34

对于vue-router 2.x的

this.$router.push('path') 

对于vue-router 1.x的

this.$route.router.go('path') 
+0

的感谢!得到了答案'this。$ route.router.go' –

+0

不错。我也会更新我的答案。 –

+1

看起来像Vue2,它现在是'this。$ router' –

相关问题