patchRoutes({ routes })
修改路由。
比如在最前面添加一个 /foo 路由,
export function patchRoutes({ routes }) {routes.unshift({path: '/foo',exact: true,component: require('@/extraRoutes/foo').default,});}
比如和 render 配置配合使用,请求服务端根据响应动态更新路由,
let extraRoutes;
export function patchRoutes({ routes }) {
merge(routes, extraRoutes);
}
export function render(oldRender) {
fetch('/api/routes').then(res=>res.json()).then((res) => {
extraRoutes = res.routes;
oldRender();
})
}
//直接修改routes,不需要返回
render(oldRender: Function)
覆写 render。
比如用于渲染之前做权限校验,
import { history } from 'umi';
export function render(oldRender) {
fetch('/api/auth').then(auth => {
if (auth.isLogin) { oldRender() }
else {
history.push('/login');
oldRender()
}
});
}
onRouteChange({ routes, matchedRoutes, location, action })
在初始加载和路由切换时做一些事情。
比如用于做埋点统计,
export function onRouteChange({ location, routes, action }) {
bacon(location.pathname);
}
比如用于设置标题,
export function onRouteChange({ matchedRoutes }) {
if (matchedRoutes.length) {
document.title = matchedRoutes[matchedRoutes.length - 1].route.title || '';
}
}
