BrowserRouter 更改 URL,但没有链接到页面。

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

BrowserRouter changing URL and not linking to page

问题

我是你的中文翻译,以下是代码的翻译部分:

在你的页面中,你引入了React的一些组件和库,以及一些路由相关的内容。你的主页 Home 组件看起来像这样:

export default function Home() {
  return (
    <div>
      <Head>
        <title>First Last Portfolio</title>
        <meta name="description" content="Text"></meta>
        <link rel="icon" href="/faviocon.ico" />
      </Head>
      <main className='bg-teal-400 px-10 md:px-20 lg:px-30'>
        <section className="bg-teal-400 min-h-screen">
          <nav className='py-10 mb-12 flex justify-between'>
            <h1 className='text-xl font-mono'>Name</h1>
            <ul className='flex items-center'>
              <li>
                <BsFillMoonStarsFill className='cursor-pointer text-2xl' />
              </li>
              <li>
                <BrowserRouter>
                  <Link className='bg-teal-950 text-gray-200 px-4 py-2 rounded-md ml-6' target="_blank" rel="noopener noreferrer" to="/resume.pdf">
                    Resume
                  </Link>
                  <Link className='bg-teal-950 text-gray-200 px-4 py-2 rounded-md ml-6' to="/projects">
                    Portfolio
                  </Link>
                </BrowserRouter>
              </li>
            </ul>
          </nav>
          <div className='text-center p-10'>
            <h2 className='text-5xl py-2 text-teal-950 font-medium md:text-6xl'>First Last</h2>
            <h3 className='text-2xl py-2 md:text-3xl'>Software Engineer</h3>
            <p className='text-md py-5 leading-8 text-gray-800 md:text-xl max-w-lg mx-auto'>blah blah blah这是我做的事情</p>
          </div>
          <div className='text-5xl flex justify-center gap-16 py-3 text-teal-950'>
            <AiFillLinkedin />
            <AiFillGithub />
          </div>
        </section>
        <section>
          <div style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
            <div style={{ flex: 1, height: '1px', backgroundColor: 'black' }} />
            <div>
              <h3 className='text-lg font-medium' style={{ width: '100px', textAlign: 'center' }}>Portfolio</h3>
            </div>
            <div style={{ flex: 1, height: '1px', backgroundColor: 'black' }} />
          </div>
          <div className='text-center shadow-lg p-10'>
            <h3 className='text-lg font-medium'>Project 1</h3>
            <p>这里应该有关于项目的一些信息</p>
          </div>
        </section>
      </main>
    </div>
  );
}

你的主页看起来已经设置了路由,但如果你的路由仍然无法正常工作,可能需要检查以下几点:

  1. 确保你的路由库正确导入和使用。在你的 App 组件中,你已经使用了 react-router-domBrowserRouterRoutes 组件,这是正确的。

  2. 确保你的路由路径与链接路径匹配。在你的主页组件中,你使用了以下链接:

    • /resume.pdf:这个链接应该指向你的简历 PDF 文件。
    • /projects:这个链接应该指向你的项目页面。
  3. 检查路由是否正确嵌套。在你的 App 组件中,你已经设置了 / 路由,并在其中包含了子路由 /resume/projects。这看起来是正确的。

如果你仍然遇到问题,可能需要检查你的路由配置和组件是否正确导入。如果问题仍然存在,请提供更多关于错误消息和问题的详细信息,以便我可以提供更具体的帮助。

英文:

Im new to js and React and have been trying to create a personal website. I have been struggling for hours simply trying to create a separate page where I can put my projects and link to it from my main page. The resume link works but only beacuse im directly linking to a pdf.

This is how my page.js looks:

&#39;use client&#39;
import Head from &#39;next/head&#39;;
import {BsFillMoonStarsFill} from &#39;react-icons/bs&#39;;
import {AiFillLinkedin, AiFillGithub} from &quot;react-icons/ai&quot;;
import { BrowserRouter, Link } from &#39;react-router-dom&#39;;
import { createContext } from &#39;react&#39;
const Context = createContext()
export default function Home() {
return (
&lt;div&gt;
&lt;Head&gt;
&lt;title&gt;First Last Portfolio&lt;/title&gt;
&lt;meta name = &quot;description&quot; content = &quot;Text&quot;&gt;&lt;/meta&gt;
&lt;link rel = &quot;icon&quot; href = &quot;/faviocon.ico&quot; /&gt;
&lt;/Head&gt;
&lt;main className=&#39;bg-teal-400 px-10 md:px-20 lg:px-30&#39;&gt;
&lt;section className=&quot; bg-teal-400 min-h-screen&quot;&gt;
&lt;nav className=&#39;py-10 mb-12 flex justify-between&#39;&gt;
&lt;h1 className=&#39;text-xl font-mono&#39;&gt;Name&lt;/h1&gt;
&lt;ul className=&#39;flex items-center&#39;&gt;
&lt;li&gt;
&lt;BsFillMoonStarsFill className=&#39;cursor-pointer text-2xl&#39;/&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;BrowserRouter&gt;
&lt;Link className=&#39;bg-teal-950 text-gray-200 px-4 py-2 rounded-md ml-6&#39; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;  to=&quot;/resume.pdf&quot;&gt;
Resume
&lt;/Link&gt;
&lt;Link className=&#39;bg-teal-950 text-gray-200 px-4 py-2 rounded-md ml-6&#39; to=&quot;/projects&quot;&gt;
Portfolio
&lt;/Link&gt;
&lt;/BrowserRouter&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;div className=&#39;text-center p-10&#39;&gt;
&lt;h2 className=&#39;text-5xl py-2 text-teal-950 font-medium md:text-6xl&#39;&gt;First Last&lt;/h2&gt;
&lt;h3 className=&#39;text-2xl py-2 md:text-3xl&#39;&gt;Software Engineer&lt;/h3&gt;
&lt;p className=&#39;text-md py-5 leading-8 text-gray-800 md:text-xl max-w-lg mx-auto&#39;&gt;blah blah blas this is what I do&lt;/p&gt;
&lt;/div&gt;
&lt;div className=&#39;text-5xl flex justify-center gap-16 py-3 text-teal-950&#39;&gt;
&lt;AiFillLinkedin /&gt;
&lt;AiFillGithub /&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section&gt;
&lt;div style={{display: &#39;flex&#39;, flexDirection: &#39;row&#39;, alignItems: &#39;center&#39;}}&gt;
&lt;div style={{flex: 1, height: &#39;1px&#39;, backgroundColor: &#39;black&#39;}}/&gt;
&lt;div&gt;
&lt;h3 className=&#39;text-lg font-medium&#39; style={{width: &#39;100px&#39;, textAlign: &#39;center&#39;}}&gt;Portfolio&lt;/h3&gt;
&lt;/div&gt;
&lt;div style={{flex: 1, height: &#39;1px&#39;, backgroundColor: &#39;black&#39;}}/&gt;
&lt;/div&gt;
&lt;div className=&#39;text-center shadow-lg p-10&#39;&gt;
&lt;h3 className=&#39;text-lg font-medium&#39;&gt;Project 1&lt;/h3&gt;
&lt;p&gt;
Some information about the project should probably be here
&lt;/p&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;/main&gt;
&lt;/div&gt;
);
}

This is how my index.js looks:


import ReactDOM from &quot;react-dom&quot;;
import {BrowserRouter as Router, Link, Route, Routes } from &#39;react-router-dom&#39;;
import Layout from &quot;./layout&quot;;
import Home from &quot;./page&quot;;
import Resume from &quot;./resume&quot;;
import Projects from &quot;./projects&quot;;
//import { BrowserRouter } from &quot;react-router-dom&quot;;
export default function App() {
return (
&lt;BrowserRouter&gt;
&lt;Routes&gt;
&lt;Route path=&quot;/&quot; element={&lt;Layout /&gt;}&gt;
&lt;Route index element={&lt;Home /&gt;} /&gt;
&lt;Route path=&quot;/resume&quot; element={&lt;Resume /&gt;} /&gt;
&lt;Route path=&quot;/projects&quot; element={&lt;Projects /&gt;} /&gt;   
&lt;/Route&gt;               
&lt;/Routes&gt;
&lt;/BrowserRouter&gt;
);
}
const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(&lt;App /&gt;);

and this is my projects.js:

import React from &quot;react&quot;;
const Projects = () =&gt; {
const projects = [
{ id: 1, title: &quot;Project 1&quot;, description: &quot;Description of Project 1&quot; },
{ id: 2, title: &quot;Project 2&quot;, description: &quot;Description of Project 2&quot; },
];
return (
&lt;div&gt;
&lt;h1&gt;Projects&lt;/h1&gt;
&lt;ul&gt;
{projects.map((project) =&gt; (
&lt;li key={project.id}&gt;
&lt;h2&gt;{project.title}&lt;/h2&gt;
&lt;p&gt;{project.description}&lt;/p&gt;
&lt;/li&gt;
))}
&lt;/ul&gt;
&lt;/div&gt;
);
};
export default Projects;

I was initially getting "Cannot use link outside router", which is fixed now but my im not getting routed to my pages.

答案1

得分: 1

恭喜你学习了React和JavaScript。

以下是翻译好的部分:

  • React是一个庞大的社区,拥有许多不同的框架和工具。它们并不都兼容。
  • Next.js是一种将服务器端渲染和React.js混合使用的工具。它与许多专注于纯客户端内容的工具不兼容。
  • 你安装了'react-router-dom',但它与Next.js不兼容,因为它侧重于客户端。
  • 这个Next.js页面将向你展示如何在他们的框架内进行路由。你可以使用这个来创建新页面,并了解新页面如何与路由连接。
  • 这个链接将向你展示如何添加到另一个页面的链接
  • 我建议删除"react-router-dom"包,并完全专注于使用Next.js工具。许多组件库可以使用,但对于Next.js,路由确实需要按Next.js的方式进行处理。
英文:

Good on you for learning react and javascript.

A couple things here

  • React is a big community with lots of different frameworks and tools. They don't all work together

  • Next.js is a type of tool that mixes server side rendering and react.js. It is incompatible with many tools that focus on pure client side stuff

  • You installed 'react-router-dom', which won't work with next.js because it is client side focused

  • This next.js page will show you how to do routing within their framework. You can use this to create new pages and see how that new page connects to the route

  • This link will show you how to add a link to another page

  • I would recommend removing the "react-router-dom" package and focusing completely on next.js tools.

  • Many component libraries will work, but for next.js, routing definitely needs to be done the next.js way

huangapple
  • 本文由 发表于 2023年6月12日 14:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453954.html
匿名

发表评论

匿名网友

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

确定