我有以下定义:
IRoute:
export interface IRoute{
key: string,
path: string,
exact: boolean,
component: JSX.Element,
private?: boolean
}
IRoutesConfig:
import { IRoute } from "./IRoute";
export default interface IRoutesConfig {
[key : string] : [IRoute]
}
和我的Routes Configration,然后我将传递给一个类进行进一步的增强
import Login from "../container/Auth/Login/Login";
import IRoutesConfig from "./IRoutesConfig";
const Routes : IRoutesConfig = {
"Auth": [
{
key: "login-page",
exact: true,
component: Login,
path: "/auth/login"
}
]
}
export default Routes;
我正在使用登录功能组件进行测试:
import React, { FC, ReactElement } from 'react';
type LoginProps = {
}
const Login : FC<LoginProps> = (props) : ReactElement => {
return (
<>
</>
);
}
export default Login;
这是一个新的项目设置,尚未为登录道具定义任何内容。因此,它目前为空,以后将进行扩展,内容也一样,因为我们当前正在设置路线体系结构。
VSCode给我以下有关“ component ”的提示Routes:IRoutesConfig
:
当然,我不是在声明类型,道具和键。
在结束路由配置将给予一个RouteManager,将动态地创建Routes
为Switch
的`阵营,路由器-DOM”,再加上一些其他的东西,不同的地方访问它。
它将创建路线图,例如:
<Route key="login-page" path="/auth/login" component={Login} />
然后在反应容器组件中使用它:
...
let authRoutes = RoutesManager.getAllRoutes("Auth");
return (
<Switch>
{authRoutes}
<Redirect key="account-redirect" to="/account" />
</Switch>
);
问题:根据我的理解,react-router-dom route
期望它是arg component
a JSX.element
,但是我的Login功能组件的返回类型是ReactElement。据我所知,JSX.element在此上下文中是通用的ReactElement。Login FC将有孩子。
如何解决将登录FC正确传递到配置,以便稍后由react-router-dom的路由组件使用的问题?
据我了解,错误是来自component: JSX.Element
。而且您正在尝试通过向中添加private
属性来创建受保护的路由Route
。您可以更改IRoute
接口以扩展react-router
定义的类型,这样您就不会再遇到此问题了。
首先:添加此导入语句。
import { RouteProps } from "react-router-dom";
第二:更改您的IRoute
界面:
export interface IRoute extends RouteProps{
private?: boolean
}
这样,您只需告诉我myIRoute
具有所有简单Route
属性plus private
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句