3 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
要將任何視圖設(shè)為默認(rèn)視圖,您可以將以下內(nèi)容添加到導(dǎo)航中
<Route path="/" exact component={() => <Home />} />
或者你可以寫如下:
<Redirect from="/" to="/hypstats"} />
<Router>
<Navigation />
<Switch>
<Route path="/hypstats" exact component={() => <Home />} />
<Route path="/hypstats/auctions" exact component={() => <AuctionViewer />} />
<Route path="/hypstats/bazaar" exact component={() => <BazaarViewer />} />
**<Route path="/" exact component={() => <Home />} />**
</Switch>
</Router>

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
我們使用 basename 屬性來(lái)告訴站點(diǎn)的基本名稱。/hypstats這樣,在接下來(lái)的路由中,每次添加新路由時(shí),我們就不必在此處手動(dòng)設(shè)置基本名稱。React Router 自己管理它。
<Router basename="/hypstats">
<Navigation />
<Switch>
<Route path="/" exact component={() => <Home />} />
<Route path="/auctions" exact component={() => <AuctionViewer />} />
<Route path="/bazaar" exact component={() => <BazaarViewer />} />
</Switch>
</Router>

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
在您的應(yīng)用程序組件中的某個(gè)位置運(yùn)行它。
history.push({ pathname: '/hypstats', });
您正在使用“react-router-dom”,因此您可以導(dǎo)入:
import { useHistory } from "react-router-dom";
然后你可以使用:
const history = useHistory();
因此,每當(dāng)用戶進(jìn)入您的 React 應(yīng)用程序時(shí),它都會(huì)打開“/hypstats”。
您可以在 useEffect 中執(zhí)行此操作。
添加回答
舉報(bào)