【问题标题】:Nesting routes and dynamically routing in React-router v4React-router v4 中的嵌套路由和动态路由
【发布时间】:2017-06-08 11:08:19
【问题描述】:

我有以下路由配置:

return (<div>
        <Router>
          <div>

            <Route path='/login' component={LoginPage}/>
            <EnsureLoggedInContainer>
              <Route path='/abc' component={abc} />
            </EnsureLoggedInContainer>
          </div>
        </Router>
      </div>
);

EnsureLoggedInContainer 是:

import React from 'react';
import { connect } from "react-redux";

class EnsureLoggedInContainer extends React.Component
{
    componentDidMount() {
        if ( !this.props.isLoggedIn )
        {
            // this.props.history.push('/login');
            this.context.router.push('/contact');

        }
    }

    render() {
        // console.log(this.props);
        if ( this.props.isLoggedIn )
        {
            return this.props.children;
        }
        else
        {
            return null;
        }
    }


}
const mapStateToProps = (state,ownProps) => {
    return{
        isLoggedIn : state.isLoggedIn,
        // currentURL : this.props
    }
}

export default connect(mapStateToProps)(EnsureLoggedInContainer);

但是,历史推送:this.props.history.push('/login'); 不起作用。这里没有历史记录。

如果我使用这样的配置:

<Route component={EnsureLoggedInContainer}>
              <Route path='/myjs' component={MyjsPage} />
            </Route>

我遇到了这样的问题:

Warning: You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored

reactjs 中最好的身份验证方式是什么?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    根据我对您的 React 路由器设计的了解,您似乎使用的是 React 路由器版本 4

    在这种情况下,您可以在组件本身中指定路由,并使用 withRouter 进行动态重定向,例如

    return (<div>
            <Router>
              <div>
    
                <Route path='/login' component={LoginPage}/>
                <EnsureLoggedInContainer/>
              </div>
            </Router>
          </div>
    );
    

    import React from 'react';
    import { connect } from "react-redux";
    import {withRouter} from "react-router";
    
    class EnsureLoggedInContainer extends React.Component
    {
        componentDidMount() {
            if ( !this.props.isLoggedIn )
            {
                this.props.history.push('/login');
    
            }
        }
    
        render() {
            // console.log(this.props);
            if ( this.props.isLoggedIn )
            {
                return <Route path='/abc' component={abc} />
            }
            else
            {
                return null;
            }
        }
    
    
    }
    const mapStateToProps = (state,ownProps) => {
        return{
            isLoggedIn : state.isLoggedIn,
            // currentURL : this.props
        }
    }
    
    export default connect(mapStateToProps)(withRouter(EnsureLoggedInContainer));
    

    【讨论】:

    • 我在EnsureLoggedInContainer 中有多个路由。我可以从中返回多条路线吗?
    • 是的,你可以,将它们包装在一个 div 中
    • 我明天试试,如果可行,我会接受作为答案。不过感谢您的帮助。
    • 您好,您的解决方案工作正常,但 url 更改为 root,即当我单击 localhost/abc 时,它加载 abc 但 url 更改为 localhost。我想保留它localhost/abc
    • 你能确保你没有在abc组件中重定向
    猜你喜欢
    • 2017-06-25
    • 2017-08-28
    • 1970-01-01
    • 2017-11-11
    • 2019-01-07
    • 1970-01-01
    • 2022-08-14
    • 2017-07-04
    相关资源
    最近更新 更多