Next.js从/重定向到另一个页面

html React.js

小胖

2020-03-23

我是Next.js的新手,并且想知道如何从起始页(/重定向/ hello-nextjs用户加载页面后,确定路径是否=== /重定向至/ hello-nextjs

react-router中,我们执行以下操作:

<Switch>
  <Route path="/hello-nextjs" exact component={HelloNextjs} />
  <Redirect to="/hello-nextjs" /> // or <Route path="/" exact render={() => <Redirect to="/hello-nextjs" />} />
</Switch>

第2620篇《Next.js从/重定向到另一个页面》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Winter 2020.11.30

服务端渲染的话在getInitialProps中重定向的办法

getInitialProps(context) {
  const {res} = context;
  if(process.browser) {
    return Router.push(url);
  } else {
    res.redirect(url)
    res.end()
    return {}
  }
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android