Reactjs 如何在typescript中将服务实例传递给react路由器组件

Reactjs 如何在typescript中将服务实例传递给react路由器组件,reactjs,typescript,react-router,Reactjs,Typescript,React Router,我们有以下代码: //routes.tsx const auth = new AuthService(); export default <Route component={ Layout }> <Route path='/login' components={{body:Login}} </Route>; //layout.tsx export interface LayoutProps { body: React.ReactElement&l

我们有以下代码:

//routes.tsx
const auth = new AuthService();

export default <Route component={ Layout }>
    <Route path='/login' components={{body:Login}}
</Route>;

//layout.tsx
export interface LayoutProps {
    body: React.ReactElement<any>
}
//routes.tsx
const auth=new AuthService();
导出默认值

我最后做的是:

export const auth=new AuthService()

在AuthService文件中。同时,我不会导出该类型


这样,任何需要auth服务的类只需从文件中导入实例,就无法创建另一个实例

也许这不是最佳做法,但您可以从routes文件导出AuthService
export const auth=new AuthService()
,然后在layout.tsx
import{auth}from./routes'