动态路由 ReactJS

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

Dynamic routes ReactJS

问题

这是用于渲染路由的组件部分:

import { useParams } from 'react-router-dom';
function Profile() {
  const { nickname } = useParams();

  return <h1> {nickname} Profile Page</h1>;
}

export default Profile;

这是路由部分:

import Home from '../pages/Home';
import Following from '../pages/Following';
import Profile from '../pages/Profile';
import Upload from '../pages/Upload';
import Search from '../pages/Search';
import { HeaderOnly } from '../layouts';

const publicRoutes = [
  {
    path: '/',
    component: Home,
  },
  {
    path: '/following',
    component: Following,
  },
  {
    path: '/@:nickname',
    component: Profile,
    layout: HeaderOnly,
  },
  {
    path: '/upload',
    component: Upload,
    layout: HeaderOnly,
  },
  {
    path: '/search',
    component: Search,
    layout: HeaderOnly,
  },
];

const privateRoutes = [];

export { privateRoutes, publicRoutes };

你想要在访问/@mynickname时渲染Profile组件。你提到使用了动态路由,但是当你尝试访问/@mynickname时,它不起作用,但如果你访问/@:nickname,它可以正常工作。

要让/@mynickname能够渲染Profile组件,你可以更改路由配置,将path设置为/@:nickname,然后在组件中使用useParams来获取nickname参数。这样,当你访问/@mynickname时,它应该能够正确渲染Profile组件。

如果你需要更多帮助,请提供更多关于你的路由配置和问题的信息。

英文:

Here is the component to render routes:

import { useParams } from &#39;react-router-dom&#39;;
function Profile() {
  const { nickname } = useParams();

  return &lt;h1&gt; {nickname} Profile Page&lt;/h1&gt;;
}

export default Profile;

Here is routes

import Home from &#39;../pages/Home&#39;;
import Following from &#39;../pages/Following&#39;;
import Profile from &#39;../pages/Profile&#39;;
import Upload from &#39;../pages/Upload&#39;;
import Search from &#39;../pages/Search&#39;;
import { HeaderOnly } from &#39;../layouts&#39;;
////

const publicRoutes = [
  {
    path: &#39;/&#39;,
    component: Home,
  },
  {
    path: &#39;/following&#39;,
    component: Following,
  },
  {
    path: &#39;/@:nickname&#39;,
    component: Profile,
    layout: HeaderOnly,
  },
  {
    path: &#39;/upload&#39;,
    component: Upload,
    layout: HeaderOnly,
  },
  {
    path: &#39;/search&#39;,
    component: Search,
    layout: HeaderOnly,
  },
];

const privateRoutes = [];

export { privateRoutes, publicRoutes };

I want my Profile Component to be rendered when accessing /@mynickname. I am using dynamic router and when I try to access /@mynickname, it does not work, but if I access /@:nickname, it works.

How can I access /@mynickname and have my Profile Component rendered? Where did I go wrong?
localhost/@johndee -> johndee Profile Page

答案1

得分: 1

这被称为URL参数,您可以很容易地执行它。

在此处阅读更多信息:https://v5.reactrouter.com/web/example/url-params

@:something 在我看来不起作用,尝试使用 :name 并使用该值。

英文:

This is called url params, you can do it very easily.

Read about this here: https://v5.reactrouter.com/web/example/url-params

@:something won't work in my opinion, try :name and use the value.

答案2

得分: 0

只返回翻译好的部分:

it&#39;s so easy

{
   path: &#39;/@:nickname&#39;,
   component: Profile,
   layout: HeaderOnly,
 },

要访问它,只需编写 `/@任何内容`,如果您编写类似 `/任何内容` 的内容,它将不起作用。
英文:

it's so easy

 {
    path: &#39;/@:nickname&#39;,
    component: Profile,
    layout: HeaderOnly,
  },

To access it, just write /@anythings, if you write something like: /anythings, it won't work.

答案3

得分: 0

问题可能出在 react-router-dom 版本上。我正在使用最新版本,开发人员可能已更改语法。当我使用较旧版本时,问题得到解决。

英文:

I think the issue lies with the version of react-router-dom. I'm using the latest version and the developer may have changed the syntax. When I use an older version, the problem is resolved.

huangapple
  • 本文由 发表于 2023年3月7日 17:38:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660204.html
匿名

发表评论

匿名网友

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

确定