下一步13个应用程序目录路由和编程式导航?

huangapple go评论75阅读模式
英文:

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 -->

&quot;use client&quot;

import { Box, Card, CardActionArea, CardContent, Typography } from &quot;@mui/material&quot;
import theme from &quot;@/theme/theme&quot;
import { usePathname, useRouter } from &quot;next/navigation&quot;

const ItemCard = (props) =&gt; {
    
    const defaultTitle = &quot;Card Title&quot;
    const defaultText = &quot;Card Body&quot;
    const defualtURL = &quot;#&quot;
    
    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, &quot;_&quot;).toLowerCase()

    const handleClick = (e) =&gt; {
        console.log(`${pathname}/${cardId}`)
        router.push(`${pathname}/${cardId}`)
    }

    return (
        &lt;Box&gt;
            
            &lt;Card elevation={2}&gt;

                    &lt;CardContent&gt;
                    
                        &lt;CardActionArea onClick={handleClick}&gt;
                        
                            &lt;Box sx={{ 
                                bgcolor: theme.palette.secondary.main,
                                color: theme.palette.common.white,
                                p: 1,
                                width: &quot;100%&quot;,
                                &quot;&amp;:hover&quot;: {
                                    bgcolor: theme.palette.primary.main,
                                },
                            }}&gt;

                            &lt;Typography
                                id={cardId}
                                component={&quot;typography&quot;}
                                variant=&quot;h6&quot;
                                sx={{
                                    color: theme.palette.common.white,
                                    display: &quot;flex&quot;,
                                    justifyContent: &quot;center&quot;,
                                    textDecoration: &quot;none&quot;, 
                                }}
                            &gt;
                                {cardTitle}
                            &lt;/Typography&gt;

                            
                            &lt;/Box&gt;

                        &lt;/CardActionArea&gt;

                        &lt;Typography component={&quot;div&quot;} variant=&quot;body2&quot; sx={{ pt: 1 }}&gt;
                            
                            { cardText }

                        &lt;/Typography&gt;

                    &lt;/CardContent&gt;

            &lt;/Card&gt;

        &lt;/Box&gt;
    )
}

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.

huangapple
  • 本文由 发表于 2023年7月14日 03:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定