在React搭建的SPA項(xiàng)目中頁(yè)面的title是直接寫(xiě)在入口index.html中,當(dāng)路由在切換不用頁(yè)面時(shí),title是不會(huì)動(dòng)態(tài)變化的。那么怎么讓title隨著路由的切換動(dòng)態(tài)變化呢?
1.在定義路由時(shí)增加title屬性。
{ path: "/regularinvestment", component: Loadable({ loader: () => import('./../../business/Regularinvestment/index'), loading: PageLoading }), title: "這是自定義的標(biāo)題" }2.在路由的index.js獲取到自定義的title設(shè)置頁(yè)面標(biāo)題即可。
const RouteWithSubRoutes = route => { return ( <Route exact path={route.path} render={props => { document.title = route.title || "默認(rèn)title"; return <route.component {...props} routes={route.routes}></route.component> }} /> ); }; export default () => { return allRouters.map((route, i) => { return <RouteWithSubRoutes key={i} {...route}/> }) };以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選