英文:
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 'react-router-dom';
function Profile() {
const { nickname } = useParams();
return <h1> {nickname} Profile Page</h1>;
}
export default Profile;
Here is routes
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 };
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's so easy
{
path: '/@:nickname',
component: Profile,
layout: HeaderOnly,
},
要访问它,只需编写 `/@任何内容`,如果您编写类似 `/任何内容` 的内容,它将不起作用。
英文:
it's so easy
{
path: '/@:nickname',
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论