React项目使用React-Router
<p>⒈初始化React项目(略)</p><p> 请参考 初始化一个React项目(TypeScript环境)</p>
<p>⒉集成React-Router</p>
<p> 在React世界里,公认最好用的路由是React-Router。那我们直接来集成它吧。</p>
<div class="cnblogs_code">
<pre>yarn add react-<span style="color: rgba(0, 0, 0, 1)">router history
#如果是type环境
yarn add react</span>-router @types/react-router history @types/history</pre>
</div>
<p>⒊配置React-Router</p>
<p> 在src中新建一个文件叫Router.js(如果是type环境则名称为Router.tsx):</p>
<div class="cnblogs_code">
<pre>cd src<br>cd.><span style="color: rgba(0, 0, 0, 1)">Router.js
#如果是type环境
cd.</span>>Router.tsx</pre>
</div>
<p> 代码如下:</p>
<div class="cnblogs_code">
<pre>import {createBrowserHistory} from 'history'<span style="color: rgba(0, 0, 0, 1)">;
import React from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import {Route,Router} from </span>'react-router'<span style="color: rgba(0, 0, 0, 1)">;
import App from </span>'./App'<span style="color: rgba(0, 0, 0, 1)">;
import Edit from </span>'./Edit'<span style="color: rgba(0, 0, 0, 1)">;
const history </span>=<span style="color: rgba(0, 0, 0, 1)"> createBrowserHistory()
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> () =><span style="color: rgba(0, 0, 0, 1)"> (
</span><Router history={history}>
<>
<Route exact path="/" component={App}/>
</>
</Router>
)</pre>
</div>
<p> 然后修改index.js(type环境为index.tsx)文件,将读取的组件修改为Router:</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'./index.css'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> import App from './App';</span>
import * as serviceWorker from './serviceWorker'<span style="color: rgba(0, 0, 0, 1)">;
import Router from </span>'./Router'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ReactDOM.render(<App />, document.getElementById('root'));</span>
ReactDOM.render(<Router/>,document.getElementById('root'));
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you want your app to work offline and load faster, you can change</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> unregister() to register() below. Note this comes with some pitfalls.</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> Learn more about service workers: https://bit.ly/CRA-PWA</span>
serviceWorker.unregister();</pre>
</div>
<p> 刷新一下页面,运行正常</p>
<p> 那我们再添加一个页面Edit.js(type环境为Edit.tsx),并为它配好路由:</p>
<h5>Edit.js</h5>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> () =><span style="color: rgba(0, 0, 0, 1)"> (
</span><div>Edit</div>
)</pre>
</div>
<h5>Router.js</h5>
<div class="cnblogs_code">
<pre>import {createBrowserHistory} from 'history'<span style="color: rgba(0, 0, 0, 1)">;
import React from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import {Route,Router} from </span>'react-router'<span style="color: rgba(0, 0, 0, 1)">;
import App from </span>'./App'<span style="color: rgba(0, 0, 0, 1)">;
import Edit from </span>'./Edit'<span style="color: rgba(0, 0, 0, 1)">;
const history </span>=<span style="color: rgba(0, 0, 0, 1)"> createBrowserHistory()
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> () =><span style="color: rgba(0, 0, 0, 1)"> (
</span><Router history={history}>
<>
<Route exact path="/" component={App}/>
<Route path="/edit" component={Edit}/>
</>
</Router>
)</pre>
</div><br><br>
来源:https://www.cnblogs.com/fanqisoft/p/12022163.html
頁:
[1]