1 回答
TA贡献1828条经验 获得超3个赞
您必须将浏览器路由器包装器移至主应用程序根目录。这就是反应应用程序路由的配置方式。然后,您可以在 comp2 中使用<Link to="/comp2"> Comp2 </Link>内部 comp1 并正确导航到 comp2,而无需使用 comp1 标头。
import React from "react"
import { render } from "react-dom"
import { BrowserRouter, Switch, Route } from "react-router-dom"
import Home from "./Home"
import Comp1 from "./Comp1"
import Comp1 from "./Comp2"
const App = () => (
<BrowserRouter>
<Switch>
<Route path="/" component={Home} exact />
<Route path="/comp1" component={Comp1} exact />
<Route path="/comp2" component={Comp2} exact />
</Switch>
</BrowserRouter>
)
补偿1
export default class Comp1 extends Component{
....
render(){
return(
<h1>Comp1</h1>
<div> In comp1 </div>
<Link to="/comp2"> Comp2 </Link>
)
}
}
添加回答
举报