问一个关于react-router的问题

这样配置好以后,每次调用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>


四季花海
浏览 806回答 2
2回答

holdtom

你可以在把你的<Route> 外面包一层<Switch>同时把你的 Redirect 放到最后面

波斯汪

<Router>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Nav/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/" render={() => (<Redirect to="/home"/>)}></Route >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/comment" component={CommentBox}></Route>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/home" component={Home}></Route>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/productList" component={ProductList}></Route>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/showdetail" component={ShowDetail}></Route>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/showdetail/:id" component={ShowDetail}></Route>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/login" component={Login}></Route>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Route path="/private" component={Private}></Route>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </Router>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript