在我们项目的开发过程中除了在我们的组件内部进行路由跳转再就是通过全局配置路由进行跳转。这俩个大家应该都会,笔者在这就不啰嗦了。
今天要说的是有特殊的情况需要在公共方法文件里进行路由跳转。
想要实现这个也很简单,只需要引入history库。
npm i history --save
而后比如说在utils文件里面使用:
import {createHashHistory } from 'history'; // hash路由// 或者import {createBrowserHistory } from 'history'; // history路由export function routerToWithParams(pathname, query) {const history = createHashHistory();history.push(pathname, query);}
这样就可以通过调用utils文件的routerToWithParams方法进行路由跳转了。
小伙伴们get到了么?