在子组件React.js中添加路由

时间:2017-10-30 17:43:43

标签: javascript reactjs

我正在尝试在子组件中添加路由但不知何故它总是返回'NOT FOUND'。这是我的代码:

App.js:

class App extends Component {
  constructor(props){
    super(props);
    this.state = {};  
    this.handleLogout = this.handleLogout.bind(this); 
  }

  handleLogout(){
    sessionStorage.clear(); 
    window.location.replace('/login'); 
  }

  componentWillMount(){
      if(sessionStorage.getItem('access_token') != null && sessionStorage.getItem('id_token') != null){
          this.setState({loggedIn: true}); 
          console.log(sessionStorage.getItem('id_token')); 
      }
      else{
          this.setState({loggedIn: false}); 
      } 
  }
  render() {
    return (
        <BrowserRouter>
          <div>
            <title>Webshop</title> 
            <NavBar loggedIn={this.state.loggedIn} />
            <Switch>
                    {/*Routes need to be include in App.js otherwise root can't find the paths*/}
                    <Route exact path='/' component={Home}/>
                    <Route exact path='/categories' component={Categories}/>
                    <Route exact path='/login' component={Login}/>
                    <Route exact path='/register' component={Register}/>

                    {(this.state.loggedIn) ? 
                    <Route exact path='/logout' render={(props) => (<Logout logOutHandler={this.handleLogout} {...props}/>)} />                    
                    : null}

                    {(this.state.loggedIn) ? 
                    <Route exact path='/profile' component={Profile} />
                    : null }

                    <Route render={function(){
                        return (<NotFound/>); 
                    }}/>
                </Switch>
              <Footer/>
          </div>  
        </BrowserRouter>
    );
  }
}

Profile.js:

class Profile extends Component {
    constructor(props){
        super(props);
        this.id = jwt_decode(sessionStorage.getItem('id_token'))['id'];
        this.getUserData = this.getUserData.bind(this);
        this.getUserData(this.id);
        this.state = {}
        console.log(this.data);   
    } 

    getUserData(_id){
       var user = new User(); 
       var userPromise = user.user_data(_id);
       userPromise.then(
           (val) => { 
               this.setState({email: val.email,
                              firstName: val.firstName,
                              lastName: val.lastName});  
           }
       )        
    }
    render(){
        return(
            <div>
                <BrowserRouter>
                <Switch>
                    <Route exact path='/address' component={UserAddress}/>
                </Switch>
                </BrowserRouter>
                <Container className='content-container'>
                    <Row>
                        <Col md={4}>
                            <Card>
                                <CardBody>
                                    <Row>
                                        <Col md={12}>
                                            <Button className='float-right' color='secondary' size='sm'>
                                                <i className='fa fa-pencil'></i>
                                            </Button>
                                        </Col>
                                        <Col md={12}>
                                            <img className='profileImage'  alt='Profile' src={logo} width={150} height={150}/>
                                            <Table>
                                                <tbody>
                                                    <tr>
                                                        <th>Name</th>
                                                        <td>{this.state.firstName}</td>
                                                    </tr>
                                                    <tr>
                                                        <th>Surname</th>
                                                        <td>{this.state.lastName}</td>
                                                    </tr>
                                                    <tr>
                                                        <th>Email</th>
                                                        <td>{this.state.email}</td>
                                                    </tr>
                                                    <tr>
                                                        <th>Password</th>
                                                        <td>********</td>
                                                    </tr>
                                                </tbody>
                                            </Table>
                                        </Col>
                                    </Row>
                                </CardBody>
                            </Card>
                        </Col>
                        <Col md={8}>
                            <Card>
                                <CardBody>
                                    <Row>
                                        <Col md={12}>

                                            <Link to='/address'>
                                            <Button className='float-right' size='sm' color='success'>
                                                <i class="fa fa-plus">
                                                </i>
                                            </Button>
                                            </Link>
                                        </Col>
                                        [Here comes a foreach loop fetching all the UserAddress, visualizing them as one card per address, with an remove and edit button]
                                    </Row>
                                </CardBody>
                            </Card>
                        </Col>
                    </Row>
                </Container>
            </div>
        )
    }
}

我想在Profile.js中添加一个路由,该路由重定向到我的新组件UserAddress,但我无法让它工作。我没有错误只返回我在App.js中定义的'NOT FOUND'消息。任何人都可以帮我解决

1 个答案:

答案 0 :(得分:0)

首先,您需要从个人资料路线中删除exact

<Route path='/profile' component={Profile} />

您的子路线应以&#39; / profile&#39;开头,因此React将呈现 Profile.js ,然后 UserAddress.js

<Route exact path='/profile/address' component={UserAddress}/>

你不需要在 Profile.js 中定义另一个 BrowserRouter

相关问题