javascript - 如何在 next.js 中实现带有重定向的链接?
问题描述
我有以下我试图在 next.js 中实现的反应组件。
反应组件:
import React from "react";
import { Route, Switch, Redirect, withRouter } from "react-router-dom";
import Dashboard from "../../pages/dashboard";
import Profile from "../../pages/profile";
function Layout(props) {
return (
<>
<Switch>
<Route
exact
path="/"
render={() => <Redirect to="/app/dashboard" />}
/>
<Route path="/app/dashboard" component={Dashboard} />
<Route path="/app/profile" component={Profile} />
</Switch>
</>
);
}
export default withRouter(Layout);
因为我对下一个很陌生。j's,我不确定,如何在 next.js 中处理带有重定向的路由,类似于上面的反应组件代码。
任何帮助表示赞赏?
解决方案
您正在使用带有 NextJS 的 React-Router,这是可能的,但不是最佳实践。
NextJS 路由器是一个相当复杂的东西,因为它同时处理客户端和服务器端路由。
您的/app/dashboard
和/app/profile
页面应该正确呈现。如果你想重定向/
到/app/dashboard
你可以在文件中使用这个技巧。getInitialProps
pages/index.js
推荐阅读
- javascript - 在 axios promise Vue.js 之后分配数据时未定义的 id
- c# - 如何以编程方式将网格列添加到 WPF .Net Core 3.1 中的特定行?
- reactjs - 如何从谷歌自动完成服务获取全球国家、州和城市名称
- c# - 如何在 WinForms C# 应用程序的 Application.Run() 期间成功退出?
- java - 在参数化测试中限制无限的参数流
- sql - 需要相当于 SQL 查询的 LINQ
- postgresql - 是否有任何说明可以帮助在 CentOS 8.1 上安装 PostGIS 3.0.0?
- python - 为什么 ffmpeg-python 无法找到我的文件?
- windows - 运行 npm install WINDOW 的作业时 c 盘中的 Jenkins mkdir 权限问题
- amazon-web-services - 即使帐户处于生产状态,电子邮件也未在 SES 上验证