【问题标题】:creating common header and sidebar across all the page using react router使用反应路由器在所有页面上创建通用标题和侧边栏
【发布时间】:2018-04-29 20:37:45
【问题描述】:

App.js

class App extends Component {
    render() {
    return (
      <Router history={history}>
        <Switch>
          <Route path="/" exact component={Login} />
          <div>
            <Header />
            <div className="main">
              <SideBar />
              <Route
                path="/dashboard"
                exact
                component={RequireAuth(Dashboard)}
              />
              <Route path="/profile" exact component={RequireAuth(Profile)} />
            </div>
          </div>
        </Switch>
      </Router>
   );
 }
}

在这里,我希望标题和侧边栏在所有页面中通用,同时我更改路线并且我得到了结果,但我认为这不是这样做的标准方式,请建议并帮助我,我应该如何创建通用布局 一些页面集和其他页面的另一种布局,比如

 <Router history={history}>
  <Switch>
   <layout1>
    <Route path="/dashboard" exact component={RequireAuth(Dashboard)}/>
    <Route path="/profile" exact component={RequireAuth(Profile)} />
   </layout1>

   <layout2>
    <Route path="/otherinfo1" exact component={RequireAuth(OtherInfo1)}/>
    <Route path="/otherinfo2" exact component={RequireAuth(OtherInfo2)} />
   </layout2>
  </Switch>
 </Router>

我正在使用反应路由器 4.2.2

每次我登录并重定向到仪表板时,jquery 功能都不起作用,我总是需要刷新页面才能运行 jquery 相关的东西(如果可以帮助的话)

【问题讨论】:

    标签: reactjs redux react-router react-redux react-router-v4


    【解决方案1】:

    您可以为两组路由添加一个标识符并将其设置为路由路径,所有其他路由都可以进入相应的容器布局中

    <Router history={history}>
      <Switch>
       <Route path="/layout1" component={FirstContainer}/>
       <Route path="/layout2" component={SecondContainer}/>
      </Switch>
    </Router>
    

    那你FirstContainer会是这样的

    const FirstContainer = ({match}) => (
        <div>
             {/* other stuff */}
             <Route path={`${match.path}/dashboard`} exact component={RequireAuth(Dashboard)}/>
             <Route path={`${match.path}/profile`} exact component={RequireAuth(Profile)} />
             {/* other stuff */}
        </div>
    
    )
    

    SecondContainer

    const SecondContainer = ({match}) => (
        <div>
             {/* other stuff */}
             <Route path={`${match.path}/otherinfo1`} exact component={component={RequireAuth(OtherInfo1)}}/>
             <Route path={`${match.path}/otherinfo2`} exact component={RequireAuth(OtherInfo2)} />
             {/* other stuff */}
        </div>
    
    )
    

    【讨论】:

    • 我试过,但模板文字在那里不起作用“${match.path}/dashboardexact component={RequireAuth(Dashboard)}/>”
    • 我这样做了,但登录后我无法查看仪表板页面检查此代码link
    • 你登录后重定向到哪里,我的意思是你重定向到的路径是什么
    • 这是我的link 我正在重定向到仪表板
    • 你可以添加你添加重定向的部分并问另一个问题,如果会看看那个
    猜你喜欢
    • 2022-01-06
    • 2023-03-03
    • 2022-01-14
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多