我在学 Vue公司 </跨度> 路由器。我想进行编程导航(没有&lt; router-link&gt;)。我的路由器和视图:
router = new VueRouter({ 路线:[ {path:‘/ videos’,name锟:‘allVideos’,component:Videos}, {path:‘/ videos /:id / edit’,name:‘editVideo’,component:VideoEdit}, ] });
新 Vue公司 </跨度> ({ el:“#app”, 路由器, 创造了
谢谢 Sandeep Rajoria
我们找到了解决方案,需要使用 this.$route 除了 $route 在组件内部
this.$route
$route
如果你正在使用vue v2&amp; vue-router v2然后在vue-cli生成的样板方式访问路由器,例如从组件是导入路由器(在路由器/ index.js中导出)
<script> import Router from '../router';
然后在您的代码中,您可以使用路由器功能,如:
Router.push('/contacts'); // go to contacts page
在我的情况下,这些以前的解决方案对我不起作用 我做了以下
然后在你的代码中你可以使用这个
this.$router.push('/contacts');
import Vue from 'vue' import Router from 'vue-router'; Vue.use(Router) const router = new VueRouter({ routes: [ {path : '/videos', name: 'allVideos', component: Videos }, {path : '/videos/:id/edit', name: 'editVideo', component: VideoEdit }, ] }); new Vue({ el: "#app", router, created: function(){ if(!localStorage.hasOwnProperty('auth_token')) { window.location.replace('/account/login'); } this.$router.push({ name: 'allVideos' }); } })