比较路由中有无switch的区别:

代码一:

      <Router history={history}>
          <Route exact path="/" component={Login}/>
          <Route path="/home" component={Home}/>
      </Router>

如果URL是"/", 那么<Home>将都被渲染,因为它们的path全都被匹配到

代码二:

      <Router history={history}>
        <Switch>
          <Route exact path="/" component={Login}/>
          <Route path="/home" component={Home}/>
        </Switch>
      </Router>

如果URL是"/",<Switch>将会开始寻找相匹配的<Login>。<Route path="/" />将会被匹配到,紧接着 <Switch>会停止继续匹配并且渲染<Home>。

 

总结:switch作用:

<Switch>是唯一的因为它仅仅只会渲染一个路径。

相关文章:

  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2021-11-17
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2021-11-15
  • 2021-08-12
  • 2021-10-29
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案