React JS中的路由器问题

时间:2019-09-13 13:16:27

标签: javascript reactjs

我是新手。我正在路由。我发现,当我单击链接时,URL会根据所单击的链接进行更改,但不会导航到下一个屏幕:https://jsfiddle.net/v78kncLb/

import {
    BrowserRouter as Router,
    Route,
    Link,
} from 'react-router-dom'
import Demo from "./demo";

<Router>
                <div className='input-form'>
                    <form>
                        <label>Name</label><br/>
                        <input type="text" name="username" onChange={this.onChange}
                               value={this.state.username}/><br/><br/>

                        <label>Password</label>
                        <input type="text" name="password" onChange={this.onChange}
                               value={this.state.password}/><br/><br/>

                        <label>Confirm Password</label>
                        <input type="text" name="confirm" onChange={this.onChange}
                               value={this.state.confirm}/><br/><br/>

                        <label>E-mail</label><br/>
                        <input type="text" name="email" onChange={this.onChange} value={this.state.email}/><br/><br/>

                        <label>Aadhar</label><br/>
                        <input type="text" name="aadhar" onChange={this.onChange} value={this.state.aadhar}/><br/><br/>

                        <label>Pan</label><br/>
                        <input type="text" name="pan" onChange={this.onChange} value={this.state.pan}/><br/><br/>

                        <label>Voter</label><br/>

                        <Link to='./demo' className="btn btn-primary">hello</Link>
                        <Route exact path="/"/>
                        <Route path="./demo" component={Demo}/>
                    </form>
                </div>
            </Router>

3 个答案:

答案 0 :(得分:1)

您需要像Router一样在Router之后放置开关。

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'

const App = () => {
  return(
    <Router>
      <div className='app'>
        <Switch>
          <Route exact path="/"/>
          <Route path="/demo" component={Demo}/>
        </Switch>
      </div>
    </Router>
  )
}

答案 1 :(得分:0)

问题与您同在。如下更新(路径前没有点)

<Link to='./demo' className="btn btn-primary">hello</Link>
<Route exact path="/"/>
<Route path="/demo" component={Demo}/>

答案 2 :(得分:0)

删除点“。”从路径路径和链接。只是用这个 <Route path="/demo" component={Demo}/>