【发布时间】:2020-02-28 20:22:31
【问题描述】:
导航栏路线不起作用。
我有一个navbar component,它充当菜单栏。这个navbar component 路由到About component。我还有一个Route.js,我在其中为应用程序的其余部分实现了路由。 Route.js 是我正在加载 Navbar component 的位置。
它正在更新URL,但不加载路径。
Complete code can be found here:
Navbar component
import React, { Fragment } from 'react'
import {
BrowserRouter as Router,
Route,
Link,
} from "react-router-dom";
import { makeStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Divider from '@material-ui/core/Divider';
import About from '../pages/About';
const useStyles = makeStyles({
hr: {
width: '0.5px',
height: '25px',
backgroundColor: '#fff',
margin: '0px 10px',
},
a: {
color: '#ffffff',
cursor: 'pointer',
textDecoration: 'none',
},
});
const Navbar = () => {
const classes = useStyles();
return (
<Fragment>
<Router>
<AppBar position="static" width='100%'>
<Toolbar>
<Typography variant="h6" >
<Link to='/' className={classes.a}> Todo List Application </Link>
</Typography>
<Divider orientation="vertical" className={classes.hr} />
<Link to='../pages/About.js' className={classes.a}>
About
</Link>
</Toolbar>
</AppBar>
<Route path="/about.js" component={About}>
<About />
</Route>
</Router>
</Fragment>
);
}
export default Navbar;
这个AppRouter被加载到App.js
【问题讨论】:
-
直接编辑浏览器url到/about.js时,你看到About组件了吗?此外,您不想将 .js 扩展名添加到您的 url 路径,因为它看起来既尴尬又危险,所以我建议只使用 /about
-
我已使用以下建议的更改修改了我的代码。但现在,问题是它同时显示
home component和about component。检查此屏幕截图。 imgur.com/a/7x8tRX8
标签: reactjs redux react-router-dom