英文:
Next 13 app directory routing and programmatic navigation?
问题
根据您提供的信息,您有一个使用应用程序目录和MUI 5的Next.js 13项目。项目的结构如下:
./src
./src/app
./src/app/dc
./src/app/dc/admin
./src/app/dc/admin/dc_types.jsx
在上面的结构中,当我尝试以编程方式导航到以下URL时,我收到了404错误:
http://localhost:3000/dc/admin/dc_types
执行router.push()
的组件如下所示:
import { Box, Card, CardActionArea, CardContent, Typography } from "@mui/material";
import theme from "@/theme/theme";
import { usePathname, useRouter } from "next/navigation";
const ItemCard = (props) => {
// ... 其他代码 ...
const router = useRouter();
const pathname = usePathname();
const cardId = cardTitle.replace(/\s/g, "_").toLowerCase();
const handleClick = (e) => {
console.log(`${pathname}/${cardId}`);
router.push(`${pathname}/${cardId}`);
}
// ... 其他代码 ...
}
export default ItemCard;
当我查看${pathname}/${cardId}
的值时,它是/dc/admin/dc_types
。
我也无法手动输入URL。
我不确定我漏掉了什么?我显然有一些配置错误吗?或者更有可能是对应用程序路由器工作方式的误解?我相对新于Next.js。
提前感谢您的帮助。
英文:
I have a Next 13 project using the app directory and MUI 5. The structure of the project is as follows:
./src
./src/app
./src/app/dc
./src/app/dc/admin
./src/app/dc/admin/dc_types.jsx
Given the structure above, when I attempt to programmatically navigate to the following url, I am getting a 404 error:
http://localhost:3000/dc/admin/dc_types
The component that is performing the router.push() is as follows:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
"use client"
import { Box, Card, CardActionArea, CardContent, Typography } from "@mui/material"
import theme from "@/theme/theme"
import { usePathname, useRouter } from "next/navigation"
const ItemCard = (props) => {
const defaultTitle = "Card Title"
const defaultText = "Card Body"
const defualtURL = "#"
const { title, text, other } = props
const cardTitle = title !== undefined ? title : defaultTitle
const cardText = text !== undefined ? text : defaultText
const router = useRouter()
const pathname = usePathname()
const cardId = cardTitle.replace(/\s/g, "_").toLowerCase()
const handleClick = (e) => {
console.log(`${pathname}/${cardId}`)
router.push(`${pathname}/${cardId}`)
}
return (
<Box>
<Card elevation={2}>
<CardContent>
<CardActionArea onClick={handleClick}>
<Box sx={{
bgcolor: theme.palette.secondary.main,
color: theme.palette.common.white,
p: 1,
width: "100%",
"&:hover": {
bgcolor: theme.palette.primary.main,
},
}}>
<Typography
id={cardId}
component={"typography"}
variant="h6"
sx={{
color: theme.palette.common.white,
display: "flex",
justifyContent: "center",
textDecoration: "none",
}}
>
{cardTitle}
</Typography>
</Box>
</CardActionArea>
<Typography component={"div"} variant="body2" sx={{ pt: 1 }}>
{ cardText }
</Typography>
</CardContent>
</Card>
</Box>
)
}
export default ItemCard
<!-- end snippet -->
When I look at the value of the value for ${pathname}/${cardId}
it is /dc/admin/dc_types.
I am unable to type the url in manually either.
I am not sure what I am missing? I obviously have something misconfigured? Or more likely a misunderstanding of how the app router works? I'm relatively new to Next.js.
I appreciate the help in advance.
答案1
得分: 1
当在应用程序目录中工作时,您的路径是由文件夹定义的。如果您希望路由返回一个组件,那个文件必须命名为page.tsx(或.jsx)。
您需要更改您的路由:
./src/app/dc/admin/dc_types.jsx
为
./src/app/dc/admin/dc_types/page.jsx
这将路由到localhost:3000/dc/admin/dc_types/
我建议您阅读官方 Next.JS 文档中关于如何使用应用程序目录的路由部分。
英文:
When working in the app directory, your paths are defined by folders. If you want a route to return a component, that file must be named page.tsx (or .jsx).
You need to change your route:
./src/app/dc/admin/dc_types.jsx
to
./src/app/dc/admin/dc_types/page.jsx
That will route to localhost:3000/dc/admin/dc_types/
I would recommend reading through the routing section on the official Next.JS docs for how to use the app directory.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论