Javascript React路由器已声明,但不';请不要工作
您好,我希望它在Javascript React路由器已声明,但不';请不要工作,javascript,reactjs,react-router,react-router-dom,Javascript,Reactjs,React Router,React Router Dom,您好,我希望它在#/为access,但#/ipb为access passipb作为道具实体时,不使用道具呈现InstrumentPage。但它不起作用 render() { return (<React.Fragment> <HashRouter> <Switch> <Route path='/' render={(props) => <InstrumentPage {...prop
#/
为access,但#/ipb
为access passipb
作为道具实体时,不使用道具呈现InstrumentPage
。但它不起作用
render() {
return (<React.Fragment>
<HashRouter>
<Switch>
<Route path='/' render={(props) => <InstrumentPage {...props} />}/>
{Object.keys(AppConfig).forEach((property) =>
<Route path={property} render={(props) => <InstrumentPage {...props} entity={property} />} />)
}
</Switch>
</HashRouter>
</React.Fragment>);
}
您需要使用
map
并返回Route
,forEach
只返回未定义的
<Switch>
<Route exact path='/' render={(props) => <InstrumentPage {...props} />}/>
{Object.keys(AppConfig).map((property) =>
<Route
path={`/${property}`}
render={ (props) => <InstrumentPage {...props} entity={property} /> }
/>
)
}
</Switch>
}/>
{Object.keys(AppConfig.map)((属性)=>
}
/>
)
}
注意:当您编写path={property}
时,这意味着path={ipb}
或path={pb}
是不正确的路径。正确的路径应该是path=“/ipb”
(注意斜杠),为此,您需要执行/${property}
[包含回勾](使用)
同时为您的第一条路线添加exact
道具
,否则它将匹配所有路线
forEach
不会返回任何内容,您的意思是将映射到阵列上吗?
<Switch>
<Route exact path='/' render={(props) => <InstrumentPage {...props} />}/>
{Object.keys(AppConfig).map((property) =>
<Route
path={`/${property}`}
render={ (props) => <InstrumentPage {...props} entity={property} /> }
/>
)
}
</Switch>