1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
您必須將瀏覽器路由器包裝器移至主應(yīng)用程序根目錄。這就是反應(yīng)應(yīng)用程序路由的配置方式。然后,您可以在 comp2 中使用<Link to="/comp2"> Comp2 </Link>內(nèi)部 comp1 并正確導(dǎo)航到 comp2,而無需使用 comp1 標(biāo)頭。
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>
)
補(bǔ)償1
export default class Comp1 extends Component{
....
render(){
return(
<h1>Comp1</h1>
<div> In comp1 </div>
<Link to="/comp2"> Comp2 </Link>
)
}
}
添加回答
舉報(bào)