在Js文件中调用vue router方法

在js中调用vue router的路由方法和react有所差异,react中不能直接调(之前项目遇到的情况),而vue中通过引入路由,然后直接调用即可。

具体步骤

1 将定义好的路由导出

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
})
export default router

2 在需要使用路由方法的页面中引入路由,然后直接调用使用

import router from '../router'

export function logout() {
  window.sessionStorage.clear()
  window.localStorage.clear()
  router.replace({ path: "/login" })
}
posted @ 2020-12-16 11:56  泽海的最佳实践  阅读(8534)  评论(0)    收藏  举报