我使用 react 创建了一个电子学习应用程序,并希望将路线拆分给许多用户(学生、教师和管理员),因此我使用了这种方法
新路由.js
const NewRoute = ({
component: Component,
type: type,
...rest
}) => (
<Route
{...rest}
render={ async props => {
console.log(await CT.checkType(type));
return CT.checkType(type) === false ? (
<Redirect to='/login' />
) : (
<Component {...props} />
)
}
}
/>
);
检查类型.js
export default {
checkType: async (type) => {
try{
const res = await axios.get('/api/check/');
console.log('api response',res);
return true
} catch(err) {
console.error(err.message);
return false
}
}
}
未捕获的错误:对象作为 React 子对象无效(找到:[object Promise])。如果您打算渲染一组子项,请改用数组。
墨色风雨
相关分类