【问题标题】:How to change the route in component did mount如何更改组件中的路由确实已安装
【发布时间】:2020-09-24 06:59:47
【问题描述】:

我在渲染中使用此代码来显示标题。需要移动这个逻辑来挂载事件。如何做到这一点。

  render() {
     const viewPage = this.props.history.location.pathname.includes("/app/accountManagers/view/")
     const editPage = this.props.history.location.pathname.includes("/app/accountManagers/edit/")
     const newPage = this.props.history.location.pathname.includes("/app/accountManagers/new/")


        return (<div >
                <div className="app-wrapper">
                    <ContainerHeader match={this.props.match} title={<IntlMessages id={editPage ? "DIC_ADMIN_SETTINGS_ACCOUNT_MANAGERS_EDIT_MANAGER" : newPage ? "DIC_ADMIN_SETTINGS_ACCOUNT_MANAGERS_NEW_MANAGER" : "DIC_ADMIN_SETTINGS_ACCOUNT_MANAGERS_VIEW_MANAGER"} />} />

                </div>

【问题讨论】:

  • 你为什么不简单地使用路线而不是匹配位置
  • 根据路线需要更改我的索引页面中的标题。所以我使用了这个逻辑
  • 所以这个组件没有在上述路径之一上呈现?

标签: javascript reactjs react-redux routes components


【解决方案1】:
componentDidMount(){

    const viewPage = this.props.history.location.pathname.includes("/app/accountManagers/view/")
    const editPage = this.props.history.location.pathname.includes("/app/accountManagers/edit/")
    const newPage = this.props.history.location.pathname.includes("/app/accountManagers/new/")

    return(
            <div>
                    <div className="app-wrapper">
                            <ContainerHeader match={this.props.match} title={
                                    <IntlMessages id={editPage ? 
                                    "DIC_ADMIN_SETTINGS_ACCOUNT_MANAGERS_EDIT_MANAGER" : newPage ? 
                                    "DIC_ADMIN_SETTINGS_ACCOUNT_MANAGERS_NEW_MANAGER" : 
                                    "DIC_ADMIN_SETTINGS_ACCOUNT_MANAGERS_VIEW_MANAGER"}
                                     />
                                     }
                             />
                     </div>
             )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-25
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 2018-02-05
    相关资源
    最近更新 更多