欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React Router基礎使用

 更新時間:2017年01月17日 16:13:17   作者:imwtr  
本文主要介紹了React Router的基礎知識,具有一定的參考價值,下面跟著小編一起來看下吧

React是個技術棧,單單使用React很難構建復雜的Web應用程序,很多情況下我們需要引入其他相關的技術

React Router是React的路由庫,保持相關頁面部件與URL間的同步

下面就來簡單介紹其基礎使用,更全面的可參考 指南

1. 它看起來像是這樣

在頁面文件中

 

在外部腳本文件中

 

 

2. 庫的引入

React Router庫的引入,有兩種方式

2.1 瀏覽器直接引入

可以引用 這里 的瀏覽器版本,或者下載之后引入

然后就可以直接使用 ReactRouter 這個對象了,我們可能會使用到其中的幾個屬性

let {Router, Route, IndexRoute, Redirect, IndexRedirect, Link, IndexLink, hashHistory, browserHistory} = ReactRouter;

2.2 npm 安裝,通過構建工具編譯引入

npm install --save react-router

安裝好路由庫之后,在腳本文件中引入相關屬性

import {Router, Route, IndexRoute, Redirect, IndexRedirect, Link, IndexLink, hashHistory, browserHistory} from 'react-router';

因瀏覽器目前還不能支持import與export命令,且babel工具不會將require命令編譯,所以我們還得需要如Webpack等構建工具編譯引入

庫引入之后,在ReactDOM的render方法中,就可以使用相關的組件了

3. 路由簡單使用

最基本的,通過URL判斷進入哪個頁面(組件部件)

class First extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return <p>First</p>
 }
}
class Second extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return <p>Second</p>
 }
}
class App extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return <div></div>
 }
}
render((
 <Router history={hashHistory}>
 <Route path="/" component={App} />
 <Route path="first" component={First} />
 <Route path="second" component={Second} />
 </Router>
 ),
 document.getElementById('box')
);

首先,Router是一個容器,history屬性定義了是用何種方式處理頁面的URL

有三種:

  • browserHistory:通過URL的變化改變路由,是推薦的一種方式,但是需要在服務器端需要做一些配置(窩目前還不知怎么配)
  • hashHistory:通過#/ ,其實就像是單頁面應用中常見的hashbang方式,example.com/#/path/path.. (使用簡單,這里暫且就用這種方式)
  • createMemoryHistory:Memory history 并不會從地址欄中操作或是讀取,它能夠幫助我們完成服務器端的渲染,我們得手動創(chuàng)建history對象

然后,在容器中使用Route組件定義各個路由,通過path指定路徑(可以看到,是不區(qū)分大小寫的),通過component指定該路徑使用的組件

也可以直接在Router容器上直接用routes屬性定義各個路由,如

let routes =
 <div>
 <Route path="/" component={App} />
 <Route path="first" component={First} />
 <Route path="second" component={Second} />
 </div>;
render(<Router routes={routes} history={hashHistory}></Router>, document.getElementById('box'));

需要注意的是{routes}中只能有一個父級,所以這里加了<div>標簽

另外,路由Route也可以嵌套,在上面的例子中,嵌套起來可能更符合實際情況

需要注意的是,這里的App在父級,為了獲取子級的First與Second組件,需要在App組件中添加 this.props.children 獲取

class App extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return <div>{this.props.children}</div>
 }
}
render((
 <Router history={hashHistory}>
 <Route path="/" component={App}>
 <Route path="first" component={First} />
 <Route path="second" component={Second} />
 </Route>
 </Router>
 ),
 document.getElementById('box')
);

同樣的,可以直接在Router中用routes屬性定義路由

let routes =
 <Route path="/" component={App}>
 <Route path="first" component={First} />
 <Route path="second" component={Second} />
 </Route>;
render(<Router routes={routes} history={hashHistory}></Router>, document.getElementById('box'));

4. 路由的其他組件

除了基本的Route之外,IndexRoute、Redirect、IndexRedirect、Link、IndexLink等,顧名思義

  • IndexRoute: 在主頁面會用到,如上個例子中,在路徑"/"下我們看到的是空白頁面,可以添加默認的頁面組件用于導航
  • Link: 可以認為它是<a>標簽在React中的實現(xiàn),使用to屬性定義路徑,還可以通過activeClass或activeStyle定義active的樣式
  • IndexLink: 類似Link,推薦用來定義指向主頁面的鏈接,當然也可以隨意定義

class First extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return (
 <p>First
 <IndexLink to="/" activeStyle={{color: 'red'}}>Basic</IndexLink>
 </p>
 )
 }
}
class Second extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return <p>Second</p>
 }
}
class Basic extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return (
 <ul role="nav">
 <li><IndexLink to="/" activeStyle={{color: 'red'}}>Basic</IndexLink></li>
 <li><Link to="/first" activeStyle={{color: 'red'}}>First</Link></li>
 <li><Link to="/Second" activeClass="active">Second</Link></li>
 </ul>
 )
 }
}
class App extends Component {
 constructor(props) {
 super(props);
 }

 render() {
 return <div>
 {this.props.children}
 </div>
 }
}
render((
 <Router history={hashHistory}>
 <Route path="/" component={App}>
 <IndexRoute component={Basic} />
 <Route path="first" component={First} />
 <Route path="second" component={Second} />
 </Route>
 </Router>
 ),
 document.getElementById('box')
);
  • Redirect: 從from路徑重定向到to路徑
  • IndexRedirect: 在主頁面,直接重定向到to路徑

render((
 <Router history={hashHistory}>
 <Route path="/" component={App}>
 <IndexRoute component={Basic} />
 <IndexRedirect to="first" />
 <Redirect from="second" to="first" />
 <Route path="first" component={First} />
 <Route path="second" component={Second} />
 </Route>
 </Router>
 ),
 document.getElementById('box')
);

5. 路由的path規(guī)則

path定義的路由的路徑,在hashHistory中,它的主頁路徑是 #/

自定義Route路由通過與父Route的path進行合并,在與主頁路徑合并,得到最終的路徑

path的語法:

  • :paramName 匹配 URL 的一個部分,直到遇到下一個/、?、#
  • () 表示URL的這個部分是可選的
  • * 匹配任意字符(非貪婪模式),直到模式里面的下一個字符為止
  • ** 匹配任意字符(貪婪模式),直到下一個/、?、#為止
<Route path="/hello/:name"> // 匹配 /hello/michael 和 /hello/ryan
<Route path="/hello(/:name)"> // 匹配 /hello, /hello/michael, 和 /hello/ryan
<Route path="/files/*.*"> // 匹配 /files/hello.jpg 和 /files/hello.html
<Route path="/**/*.jpg"> // 匹配 /files/hello.jpg 和 /files/path/to/file.jpg

而:name可以通過 this.props.params 中取到

class First extends Component {
 constructor(props) {
 super(props);
 }
 render() {
 return (
 <p>First {this.props.params.name}
 <IndexLink to="/" activeStyle={{color: 'red'}}>Basic</IndexLink>
 </p>
 )
 }
}
.
.
<Route path="/:name" component={First} />

通過React Dev Tool也可以看到組件的相關數(shù)據(jù)

6. 路由的onEnter、onLeave鉤子

在路由的跳轉中,我們可能需要在進入頁面或離開頁面的時候做一些特殊操作,Route 通過 onEnter 與 onLeave 定義了這兩個行為

<Route path="first" component={First} onEnter={(nextState, replace) => {
 console.log(nextState);
 alert('onEnter');
 // replace('second');
 }} onLeave={() => {
 alert('onLeave');
 }}/>

如上,帶兩個參數(shù),通過 replace 可以更新路徑,把注釋去掉后,進入"/first"時立馬跳轉值"/second",這在檢測登錄時應該比較有用

更多的使用參見 指南

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關文章

  • JavaScript中各數(shù)制轉換全面總結

    JavaScript中各數(shù)制轉換全面總結

    這篇文章主要介紹了JavaScript中各數(shù)制轉換,利用toString的基模式來進行轉換,對數(shù)字調(diào)用 toString(10) 與調(diào)用 toString() 它們返回的區(qū)別和相同之處等等都在本文中提及,具體操作步驟大家可查看下文的詳細講解,感興趣的小伙伴們可以參考一下。
    2017-08-08
  • JavaScript編程的單例設計模講解

    JavaScript編程的單例設計模講解

    這篇文章主要介紹了JavaScript編程的單例設計模講解,單例模式編寫有利于JS代碼的維護和調(diào)試,需要的朋友可以參考下
    2015-11-11
  • javascript設計模式之工廠模式

    javascript設計模式之工廠模式

    這篇文章主要為大家介紹了javascript工廠模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • JavaScript中的原型prototype屬性使用詳解

    JavaScript中的原型prototype屬性使用詳解

    這篇文章主要介紹了JavaScript中的原型prototype屬性使用詳解,是JS入門學習中的基礎知識,需要的朋友可以參考下
    2015-06-06
  • JavaScript DOM 學習第二章 編輯文本

    JavaScript DOM 學習第二章 編輯文本

    在這一章我會給出一個在CMS里非常有用的更新頁面的代碼。在任一段落點擊鼠標你就可以修改了。完成以后點擊按鈕,修改的文本就顯示了。
    2010-02-02
  • JavaScript基本概念初級講解論壇貼的學習記錄

    JavaScript基本概念初級講解論壇貼的學習記錄

    JavaScript基本概念 論壇貼建議大家看下,都是一些js的高級的技巧知識小結。
    2009-02-02
  • JavaScript排序算法之希爾排序的2個實例

    JavaScript排序算法之希爾排序的2個實例

    希爾排序,也稱遞減增量排序算法,是插入排序的一種高速而穩(wěn)定的改進版本。希爾排序是基于插入排序的以下兩點性質(zhì)而提出改進方法的
    2014-04-04
  • JS中this的指向以及call、apply的作用

    JS中this的指向以及call、apply的作用

    本篇文章給大家分享了JS基礎內(nèi)容this指向以及call、apply的相關知識點內(nèi)容,有興趣的朋友可以學習參考下。
    2018-05-05
  • ASP.NET實現(xiàn)Repeater控件的數(shù)據(jù)綁定

    ASP.NET實現(xiàn)Repeater控件的數(shù)據(jù)綁定

    這篇文章介紹了ASP.NET實現(xiàn)Repeater控件數(shù)據(jù)綁定的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • DOM基礎教程之使用DOM設置文本框

    DOM基礎教程之使用DOM設置文本框

    這篇文章主要介紹了DOM基礎教程之使用DOM設置文本框的相關資料,需要的朋友可以參考下
    2015-01-01

最新評論