spi*_*yte 2 reactjs react-router react-dom react-router-dom
我是 React 和 React-router-dom 的新手。我已经花了几个小时了,但我却陷入了死胡同。
我正在使用 create-react-app 和 bulma 作为 css。我的依赖项:
"dependencies": {
"bulma": "^0.7.1",
"font-awesome": "^4.7.0",
"node-sass-chokidar": "^1.3.3",
"react": "^16.5.2",
"react-bulma-components": "^2.1.0",
"react-dom": "^16.5.2",
"react-fontawesome": "^1.6.1",
"react-router-dom": "4.3.1",
"react-scripts": "2.0.4"
Run Code Online (Sandbox Code Playgroud)
},
我查看了 stackoverflow、create-react-app 文档上的所有相关问题,并在其存储库中搜索了他们的开放问题部分,用谷歌搜索了每个可能的关键字。我不知所措。
我的路由器只会渲染一条路线(Homeroute)。
我如何让它也显示“/create”路线?
我试过了:
尝试了 BrowserRouter 和 HashRouter
import React, { Component } from "react";
import { Switch, Route, NavLink, BrowserRouter, HashRouter } from "react-router-dom";
const Home = () => {
return (<h2>This is Home</h2>);
}
const Create = () => {
return (<h2>This is Create page</h2>);
}
class App extends Component {
render() {
return (
<HashRouter>
<div>
<Navigation />
<div className="section">
<Switch>
<Route path="/" exact component={Home} />
<Route path="/create" exact Component={Create} />
</Switch>
</div>
<Footer />
</div>
</HashRouter>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)结果:“这就是家”
期待:“这就是家”
网址:http://localhost:3000/#/create
网址:http://localhost:3000/#/create/
结果: 什么都没有
预期:“这是创建页面”
这是一个错字。注意字母“C”,它应该是小写的。
做出改变,从
<Route path="/create" exact Component={Create} />
Run Code Online (Sandbox Code Playgroud)
到
<Route path="/create" exact component={Create} />
Run Code Online (Sandbox Code Playgroud)
这是演示: https: //codesandbox.io/s/oq0rov0r4q
| 归档时间: |
|
| 查看次数: |
2201 次 |
| 最近记录: |