【发布时间】:2021-04-18 22:01:56
【问题描述】:
我在尝试使用导航栏呈现反应组件时遇到问题。 Switch case 和 if 语句这两种方法我都试过了。
第一种方法是使用 window.location.hash,它可以工作并在点击时更改 url,但我必须刷新浏览器才能看到呈现正确的组件,即显示选定的组件。
另一种方法是定位window.location.pathname,它以所需的方式写入url,而不是呈现所需的组件,而是呈现我的主页。
这里我会展示代码。
About.jsx(带有Pathname的版本被注释掉并使用if语句。任何一个都给我同样的问题):
import React from 'react';
import '../App.css';
import AboutMe from './About/AboutMe';
import AboutMeNavigationBar from './About/AboutMeNav';
import Career from './About/Career';
import Education from './About/Education';
import Skills from './About/Skills';
export default function About(){
console.log(document.getElementsByClassName("about-nav-bar")[0]);
return(
<div>
<AboutMeNavigationBar />
{selectSection()}
</div>
);
}
function selectSection(){
var path = window.location.pathname;
var hash = window.location.hash;
console.log(window.location);
console.log(path);
console.log(window.location.hash);
switch (hash) {
case '#skills':
return <Skills />
case '#career':
return <Career />
case '#education':
return <Education />
default:
return <AboutMe />
}
// if (path === "/about/skills") {
// console.log();
// return <Skills />
// }
// else if (path === "/about/career") {
// return <Career />
// }
// else if (path === "/about/education") {
// return <Education />
// }
// else return <AboutMe />
}
AboutMeNav.jsx(带 href=#....):
import React from 'react';
import '../../App';
export default function AboutNav(props){
console.log(window.location.pathname);
console.log(window.location.hash);
return <Sections />
}
export function Sections(props){
return(
<ul className="about-nav-bar">
<Section text="ABOUT ME" href="#aboutme"></Section>
<Section text="SKILLS" href="#skills"></Section>
<Section text="CAREER" href="#career"></Section>
<Section text="EDUCATION" href="#education"></Section>
</ul>
);
}
export function Section(props){
return(
<li>
<a href={props.href}>{props.text}</a>
</li>
);
}
AboutMeNav.jsx(href="/about/aboutme" 等):
......
.........
export function Sections(props){
return(
<ul className="about-nav-bar">
<Section text="ABOUT ME" href="/about/aboutme"></Section>
<Section text="SKILLS" href="/about/skills"></Section>
<Section text="CAREER" href="/about/career"></Section>
<Section text="EDUCATION" href="/about/education"></Section>
</ul>
);
}
......
..........
来到场景二,它在浏览器中显示正确的 url,但不断将我重定向到主页。 将显示主要的 App.js 代码,因为它会根据该开关情况奇怪地重定向:
import './App.css';
import NavigationBar from './Components/Navigationbar';
import Home from './Components/Home';
import About from './Components/About';
import Contact from './Components/Contact';
function App() {
function switchPage(){
switch (window.location.pathname) {
case '/about':
return( <About />);
case '/contact':
return( <Contact />);
default:
return(<Home />);
}
}
return (
<div className="App">
<NavigationBar className="Navbar" />
<header className="App-header">
{switchPage()}
</header>
</div>
);
}
export default App;
有人可以帮我解决这些问题吗?在不使用 react-router 的情况下,我的问题有什么解决方案吗?
感谢所有帮助。非常感谢。
【问题讨论】:
标签: javascript reactjs window.location