素材牛VIP会员
问一个关于react-router的问题
 ad***cn  分类:JavaScript  人气:1827  回帖:2  发布于6年前 收藏

这样配置好以后,每次调用Route都会报错
“You tried to redirect to the same route you're currently on: "/home"”
请问该怎么优化呢?

      <Router>
        <div>
          <Nav/>
          <Redirect to="/home"></Redirect>
          <Route path="/comment" component={CommentBox}></Route>
          <Route path="/home" component={Home}></Route>
          <Route path="/productList" component={ProductList}></Route>
          <Route path="/showdetail" component={ShowDetail}></Route>
          <Route path="/showdetail/:id" component={ShowDetail}></Route>
          <Route path="/login" component={Login}></Route>
          <Route path="/private" component={Private}></Route>
        </div>
      </Router>
 标签:react.jsjavascript

讨论这个帖子(2)垃圾回帖将一律封号处理……

Lv4 码徒
逆***团 Linux系统工程师 6年前#1
<Router>
        <div>
          <Nav/>
          <Route path="/" render={() => (<Redirect to="/home"/>)}></Route >
          <Route path="/comment" component={CommentBox}></Route>
          <Route path="/home" component={Home}></Route>
          <Route path="/productList" component={ProductList}></Route>
          <Route path="/showdetail" component={ShowDetail}></Route>
          <Route path="/showdetail/:id" component={ShowDetail}></Route>
          <Route path="/login" component={Login}></Route>
          <Route path="/private" component={Private}></Route>
        </div>
      </Router>
Lv4 码徒
li***zi 其它 6年前#2

把你的<Redirect to="/home"></Redirect>放最后应该也可以。

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取