只有导航栏显示实际上与您的路由配置有关。
而不是这个:
function App() {
return (
<>
<Router>
<Navbar />
<Switch>
<Route path="/" exact component={Home} />
<Route path="/services" exact component={Services} />
<Route path="/products" exact component={Products} />
<Route path="/sign-up" exact component={SignUp} />
</Switch>
</Router>
</>
);
}
这样做:
function App() {
return (
<>
<Router>
<Navbar />
<Switch>
<Route path="/react-project" exact component={Home} />
<Route path="/react-project/services" exact component={Services} />
<Route path="/react-project/products" exact component={Products} />
<Route path="/react-project/sign-up" exact component={SignUp} />
</Switch>
</Router>
</>
);
}
由于您的主页网址后缀为 /react-project,因此您希望在路由和重定向中考虑到这一点。
因此,为了更加清晰,Navbar 中的导航 Link 组件也应该从此更改:
<Link to="/" className="navbar-logo" onClick={closeMobileMenu}>
1 TRVL 2 <i class="fab fa-typo3" />3{" "}
</Link>
到这里:
<Link to="/react-project" className="navbar-logo" onClick={closeMobileMenu}>
1 TRVL 2 <i class="fab fa-typo3" />3{" "}
</Link>
至于图片问题。
要通过src 指定 img,您可以执行以下操作:
import img1 from "../images/img-1.jpg"; // relative path from directory this component file is in
// ...
<CardItem
src={img1}
text="Explore the hidden waterfall deep inside the Amazon Jungle"
label="Adventure"
path="/services"
/>
要通过 css 将图像指定为背景图像,您可以执行以下操作:
background: url("../images/img-home.jpg"); // Also relative to current file location
来自公共 url 的路径显然不再像以前在创建反应应用程序 4 中那样解析,请参阅 this github issue。