考虑以下:
import { Route, Link, Switch } from "react-router-dom";
import { Redirect } from 'react-router'
function Index() {
return <h2>Home</h2>;
}
function About() {
return <h2>About</h2>;
}
function Users() {
return <h2>Users</h2>;
}
function NotFound() {
return <h2>Not found</h2>;
}
function App() {
return(
<div>
<h1>Welcome to Next.js!</h1>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about/">About</Link>
</li>
<li>
<Link to="/users/">Users</Link>
</li>
<li>
<Link to="/people/">People</Link>
</li>
</ul>
</nav>
<Switch>
<Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/" component={Users} />
<Redirect from="/people/" to="/users/" />
<Route component={NotFound}/>
</Switch>
</div>
);
}
export default App;
有什么可以阻止一个人这样做吗?
import { Route, Switch } from "react-router-dom";
import LinkNav from './LinkNav'
import { Redirect } from 'react-router'
function Index() {
return <h2>Home</h2>;
}
function About() {
return <h2>About</h2>;
}
function Users() {
return <h2>Users</h2>;
}
function NotFound() {
return <h2>Not found</h2>;
}
function App() {
return(
<div>
<h1>Welcome to Next.js!</h1>
<nav>
<LinkNav/>
</nav>
<Switch>
<Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/" component={Users} />
<Redirect from="/people/" to="/users/" />
<Route component={NotFound}/>
</Switch>
</div>
);
}
有什么缺点吗?