react 根据路由动态改变页面title

当然可以自己写,实现方式也很简单。在切换路由的时候去改变document.title就可以了

因为在ant design pro的项目中看到,使用了github上'react-document-title' 的这个组件,就了解了一下~

根据不同路由,更改页面title

方式:

1.引入

    import DocumentTitle from 'react-document-title';
  1. render函数顶层

     <DocumentTitle title={this.getPageTitle()}>
           <div></div>
     </DocumentTitle>
    

3.getPageTitle函数 根据当前路由更改对应的title

  getPageTitle() {
    const { location, getRouteData } = this.props;
    const { pathname } = location;
    let title = 'Ant Design Pro';
    getRouteData('BasicLayout').forEach((item) => {
      if (item.path === pathname) {
        title = `${item.name} - Ant Design Pro`;
      }
    });
    return title;
  }

至此 大功告成

github地址:https://github.com/gaearon/react-document-title

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容