问题1:
类型“Element[]”的参数不能赋给类型“Element”的参数。
类型“Element[]”缺少类型“Element”中的以下属性: type, props, key
代码:
<Switch>
{!!routes.length && <Redirect exact strict from="/" to={routes[0].path} />}
{routes.flatMap((r: MenuItem) => {
const routes = [<Route key={r.path} exact path={r.path} component={r.component} />];
if (r.children) {
const childRoutes = r.children?.map((sub: MenuItem) => {
return <Route key={sub.path} exact path={sub.path} component={sub.component} />
})
routes.push(childRoutes) // 报错如图
}
return routes;
})}
{/* <Route component={NotFound}/> */}
<Redirect from="*" to={{ pathname: '/404' }} />
</Switch>
问题2:
// 这是我的store代码
import { Dispatch, Action } from 'redux'
export interface AuthAction extends Action {
data: UserInfo
}
const reducer = (state = initialState, action: AuthAction) => {
switch(action.type){
case types.AUTHORIZE:
return { ...state, authorizeStatus: false }
case types.AUTHORIZE_SUCCESS:
return { ...state, userInfo: action.data, authorizeStatus: true }
case types.AUTHORIZE_FAILURE:
return { ...state, authorizeStatus: false}
default:
return {...state}
}
}
export default reducer;
export const authorize = (cfg: ConfigInfo) => async (dispatch: Dispatch<AuthAction>): Promise<void> => {
try {
dispatch({type: types.AUTHORIZE, data: {}});
const res = await authService.authorize(cfg);
dispatch({
type: types.AUTHORIZE_SUCCESS,
data: res.data,
});
return res.data;
} catch (err) {
dispatch({type: types.AUTHORIZE_FAILURE,data: {} });
throw err;
}
}
我在store.dispatch的时候报了这么个错误,大概是type是必须的但是我却没有,我是流程写的有问题么?
你的
routes
類型是Element
,而childRoutes
是Element[]
。你應該用concat
而不是push
。