简体   繁体   English

如何在反应路由器 dom 中使用嵌套路由

[英]How to Use nested routes in react router dom

I am trying to use nested routes with react router but its not working for me.我正在尝试将嵌套路由与反应路由器一起使用,但它对我不起作用。 I also tried to add nested router on main App but it's not working.我还尝试在主应用程序上添加嵌套路由器,但它不起作用。

App.js应用程序.js

import Message from './containers/Message'
import {connect} from 'react-redux'
import {addMessage, setMessages} from './store/actions/message'

import {
    BrowserRouter as Router,
    Route
} from "react-router-dom";

class App extends Component {

    constructor(props) {
        super(props)
        WebSocketInstance.addCallbacks(
            this.props.setMessagesMethod.bind(this),
            this.props.addMessageMethod.bind(this)
        )
    }

    render() {
        return (
            <Router>
                <Route exact path='/login/' component={WrappedLoginForm} />
                <Route exact path='/' component={ChatApp} />
                {/* <Route path='/:chatID' component={Message} /> */}
            </Router>
        )
    }
}

ChatApp.js聊天应用程序

export class ChatApp extends Component {

  componentWillMount() {
    if (!this.props.loading){
      this.props.autoLogin(this.props.history)
    }
  }

  render() {
    return (
      this.props.isAuthenticated ?
      (<div id='frame' >
        <Sidepanel />
        <div className="content">
          <div className="contact-profile">
            <img src="https://www.netfort.com/assets/user.png" alt="" />
            <p>{this.props.user.username}</p>
          </div>

          <Route path='/chat/:chatID' component={Message} />

        </div>
      </div>): null
    )
  }
}

i am trying to match http://localhost:3000/chat/1 but Message component is not rendering.我正在尝试匹配http://localhost:3000/chat/1但消息组件未呈现。

如果您使用exact path则不会命中任何子路由,因为子路由将与命中父路由的条件不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM