有权限路由,那 history.push( ‘/‘)就可能存在页面不存在
我是通过权限过滤过的路由存在全局变量(initialState)
1.在app.tsx
import routes from '../config/routes';
export async function getInitialState() {
return {
// 其他省略
routes
}
}
2、在登录成功后,根据账号的权限,跳转
// user/login.tsx组件中,登录成功后。做如下判断,进行跳转
const userInfo = await fetchUserInfo() as {[key:string]: any}
await fetchPrivilege();
const routes = initialState?.routes
if(userInfo.creator) {
history.push('/');
}else {
const role = userInfo?.role?.privilege as {[key:string]: []}
const routefilter = routes.filter(route => route.access && role[route.access]);
if(routefilter) {
// 默认跳转到第一个路由
history.push(routefilter[0].path);
return
}
}
history.push('/');
return;