当前位置: 首页 > Web前端 > HTML5

React项目动态设置标题

时间:2023-04-05 21:20:58 HTML5

React构建的SPA项目中,页面的标题直接写在入口index.html中。当路由在不同页面之间切换时,标题不会动态变化。那么如何让标题随着路由的切换而动态变化呢?1.定义路由时添加title属性。{path:"/regularinvestment",component:Loadable({loader:()=>import('./../../business/Regularinvestment/index'),loading:PageLoading}),title:"这是来自Definedtitle"}2.在路由的index.js中获取自定义title,设置页面标题。constRouteWithSubRoutes=route=>{return({document.title=route.title||“默认标题”;return}}/>);};exportdefault()=>{returnallRouters.map((route,i)=>{return})};