我使用反应路由器嵌套路由,但它不起作用

时间:2021-02-01 17:08:43

标签: javascript reactjs mern

我正在做一个嵌套路由器,但不起作用,不知道为什么,它给了我错误:

Uncaught SyntaxError: Unexpected token '<' bundle.js:1

代码在这里,这是在索引组件中:

function Index() {
return (
    <Router>
        <Switch>
            <Route exact path = "/" exact component = {Home}></Route>
            <Route path = "/register" component = {Register}></Route>
            <Route path = "/login" component = {Login}></Route>
            <Route path = "/users" component = {Users}></Route>
            <Route path = "/posts" component = {Posts}></Route>
            <Route component = {Not_found}></Route>
        </Switch>
    </Router>
)

嵌套从 /posts 开始,所以当我在 url 中输入 /posts 时,我得到组件帖子,这里是:

function Posts() {

const {path, url} = useRouteMatch();

return (
    <Switch>
        <Route exact path = {path} component = {Latests_posts}></Route>
        <Route path = {`${path}/hclick`} component = {Click}></Route>
    </Switch> 
)

组件 latests_posts 可以工作,但是当我输入 posts/click 时出现错误

0 个答案:

没有答案