Next.js链接标签正确添加到浏览器URL堆栈,但无法找到页面。

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

Next JS Link tag correctly adding to browser url stack but page cannot be found

问题

以下是您要翻译的内容:

import React, { useState } from "react";
import { createContext } from "react";
import { motion } from "framer-motion";
import { AiFillHome } from "react-icons/ai";
import { MdOutlineLightMode, MdOutlineDarkMode, MdLightMode, MdDarkMode } from "react-icons/md";
import { RiComputerLine, RiComputerFill } from "react-icons/ri";
import { CgProfile, CgLogOut } from "react-icons/cg";
import Link from "next/link";

const HeaderContext = createContext({});

type Props = {};

export default function Header({}: Props) {
    const [theme, setTheme] = useState("computer"); // 用于在图标之间切换的点击
    return (
    // 其余代码在这里,但功能正常
    <header className="sticky top-0 bg-[#FFFFFF] p-5 flex items-start justify-between mx-auto z-20 xl:items-center">
        <motion.div 
        initial={{
            x: 500,
            opacity: 0,
            scale: 0.5
        }}
        animate={{
            x: 0,
            opacity: 1,
            scale: 1
        }}
        transition={{
            duration: 1.0
        }}
        className="flex flex-row items-center cursor-pointer">
            <Link href="/login">
                <button className="heroButton">Sign In</button>
            </Link>
        </motion.div>
    </header>
  )
}

现在,您可以继续处理问题的其他部分。

英文:

I am building an app for a friend with next js (TS) 13 and currently in my header component I have a &lt;Link&gt; tag that redirects to a login page. Here is the code in the header component,

&quot;use client&quot;
import React, { useState } from &quot;react&quot;
import { createContext } from &quot;react&quot;
import { motion } from &quot;framer-motion&quot;
import { AiFillHome } from &quot;react-icons/ai&quot;
import { MdOutlineLightMode, MdOutlineDarkMode, MdLightMode, MdDarkMode } from &quot;react-icons/md&quot;
import { RiComputerLine, RiComputerFill } from &quot;react-icons/ri&quot;
import { CgProfile, CgLogOut } from &quot;react-icons/cg&quot;
import Link from &quot;next/link&quot;

const HeaderContext = createContext({})

type Props = {}

export default function Header({}: Props) {
    const [theme, setTheme] = useState(&quot;computer&quot;) {/* used for clicking between icons */}
    return (
    {/* rest of code is here but functionality is fine */}
    &lt;header className = &quot;sticky top-0 bg-[#FFFFFF] p-5 flex items-start justify-between mx-auto z-20 xl:items-center&quot;&gt;
        &lt;motion.div 
        initial = {{
            x: 500,
            opacity: 0,
            scale: 0.5
        }}
        animate = {{
            x: 0,
            opacity: 1,
            scale: 1
        }}
        transition = {{
            duration: 1.0
        }}
        className = &quot;flex flex-row items-center cursor-pointer&quot;&gt;
            &lt;Link href = {&quot;/login&quot;}&gt;
                &lt;button className = &quot;heroButton&quot;&gt;Sign In&lt;/button&gt;
            &lt;/Link&gt;
        &lt;/motion.div&gt;
    &lt;/header&gt;
  )
}

Now in the ./app directory I have a page.tsx which displays this component and another file called login.tsx. What happens when I click on this sign in button when I run the app is the url of the local server changes to localhost:3000/login which I believe should be the correct url tag that should point to the login.tsx file in the ./app directory. Here is a picture of the directory hierarchy as well:

Next.js链接标签正确添加到浏览器URL堆栈,但无法找到页面。

I have no clue what exactly is happening. I wanted to see if something is wrong with my development environment so I created a brand new next js app with npx create-next-app@latest --ts and yet the issue still persists. I looked at a similar issue posted here and the issue with that user seemed to be with the casing difference between the file name and the href value. Also before I forget this is my login page code,

import React from &quot;react&quot;

type Props = {}

export default function Login({}: Props) {
  return (
    &lt;div&gt;
      &lt;p&gt;This is a test for the login page.&lt;/p&gt;
    &lt;/div&gt;
  )
}

Changing the function name to match the href value also did nothing sadly Next.js链接标签正确添加到浏览器URL堆栈,但无法找到页面。

EDIT: Here are some of the steps I have tried to resolve the issue. I cleared the cache of my current browser, used a completely different browser. Deleted my ./node_modules folder and package-lock.json file and recompiled them with npm install.

答案1

得分: 1

我相信当你想要打开一个新页面时,你应该把所有的页面都放在"pages"文件夹中。
尝试创建一个名为"pages"的文件夹,然后把你的"login.tsx"放在那里。
如果你正在使用自定义路由或已对默认路由行为进行了修改,请确保"/login"路由被正确处理。

英文:

I believe when you want to open a new page you should add all of your pages in the pages folder.
try to create a folder called "pages", then add your "login.tsx" in there.
If you are using custom routing or have made modifications to the default routing behavior, ensure that the "/login" route is properly handled.

huangapple
  • 本文由 发表于 2023年6月5日 11:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403377.html
匿名

发表评论

匿名网友

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

确定