反应路由器嵌套路由方法

时间:2018-05-13 11:22:32

标签: reactjs react-router

React router 4官方文档有嵌套路由的例子

const App = () => (
  <BrowserRouter>
    {/* here's a div */}
    <div>
      {/* here's a Route */}
      <Route path="/tacos" component={Tacos}/>
    </div>
  </BrowserRouter>
)

// when the url matches `/tacos` this component renders
const Tacos  = ({ match }) => (
  // here's a nested div
  <div>
    {/* here's a nested Route,
        match.url helps us make a relative path */}
    <Route
      path={match.url + '/carnitas'}
      component={Carnitas}
    />
  </div>
)

我的问题是我认为使用这样的设置我可以达到同样的效果 (而不是将Carnitas放在Tacos组件中,直接写出来 在Tacos)旁边

const App = () => (
  <BrowserRouter>
    {/* here's a div */}
    <div>
      {/* here's a Route */}
      <Route path="/tacos" component={Tacos}/>
      <Route
      path={'tacos/carnitas'}
      component={Carnitas}
    />
    </div>
  </BrowserRouter>
)

// when the url matches `/tacos` this component renders
const Tacos  = ({ match }) => (
  // here's a nested div
  <div>

  </div>
)

与第一种方法相比,第二种方法的局限性是什么?

0 个答案:

没有答案