javascript - React Route - 点击链接的问题
问题描述
我有一个奇怪的问题我正在使用侧边栏,用户可以通过单击看起来像这样的特定链接转到所需的选项卡
今天我注意到我可以使用某个选项来缩短我的代码,现在我将解释一切。
目前,我的项目结构如下所示
SideBarItems.js
const SidebarItems = [
{ CourseTopic: "Introduction" },
"Introduction",
"Creating Your First Javascript",
"Comments",
"Let",
"Variables",
"Data Types",
].map((itemName) => {
if (typeof itemName === "string") {
return {
name: itemName,
CourseTopic: itemName.CourseTopic || "",
route: `/${itemName}`,
};
} else {
return {
name: itemName.name,
CourseTopic: itemName.CourseTopic,
route: `/${itemName.names}`,
};
}
});
export default SidebarItems;
路由.js
function RoutesPage(props) {
const {path} = props.path;
const {location} = props.location.pathname;
const routes = [
{path: `${path}/Introduction`, component: () => <Introduction SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Creating Your First Javascript`, component: () => <CreatingFirstJS SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Comments`, component: () => <Comments SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Let`, component: () => <Let SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Variables`, component: () => <Variables SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Data Types`, component: () => <DataTypes SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Objects`, component: () => <Objects SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Events`, component: () => <Events SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Strings`, component: () => <Strings SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/String Methods`, component: () => <StringMethods SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: `${path}/Intro to ES6`, component: () => <Intro SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
];
return (
<>
<Switch>
{routes.map((route, index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
component={route.component}
/>
))}
</Switch>
</>
);
}
export default RoutesPage;
请注意Path
,Path
将字符串“”同步,Lesson
然后我分配字符串Introduction
,,,Comments
让等等,也就是说,结果是“ Lesson/Let
”或“ Lesson/Introduction
”等等,一切对我来说都很好。我可以点击链接,一切都很棒。
但是今天我注意到没有必要手动编写路径,因为我已经将所有路径都存储在里面了location
,这是图片。
当然,我决定使用pathname
fromlocation
我适当地替换了我的代码
const {pathname} = props.location.pathname;
const routes = [
{path: pathname, component: () => <Introduction SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: pathname, component: () => <CreatingFirstJS SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
{path: pathname, component: () => <Comments SideBarThemeValue={SideBarThemeValue} AlertStyleBG={AlertStyleBG}/>},
];
之后,当我尝试点击链接时,我的 url 发生了变化,即 url 有效,但没有显示内容,我无法理解这个原因。
解决方案
推荐阅读
- excel - 添加 nlog 后 Excel 插件不再工作(与 addin-express 一起安装)
- javascript - React : Invariant Violation: 对象作为 React 子对象无效
- python - Pyinstaller EXE: FileNotFoundError: No such file or directory 'C:\\Users\\my.name\\Desktop\\EPE 2.0\\dist\\main\\timezonefinder\\timezone_names.json'
- r - 字符串匹配,其中字符串包含标点符号
- bash - bash:基本的 if 条件不起作用?(字符串比较)
- javascript - 如何触发自定义复选框小部件
- javascript - reactJs 中的组件
- javascript - 显示未捕获的引用错误
- laravel - 原始 SQL 到 Eloquent
- java - Java JRE/JDK 和 JAVA_HOME 或 java.home 的 Gradle 问题