react-router 學習筆記

来源:http://www.cnblogs.com/miaowwwww/archive/2017/01/23/6343207.html
-Advertisement-
Play Games

前言: 本文為個人學習react-router的總結。包括路由基礎配置,跳轉,許可權管理,組件與路由配置的關係,代碼分割。歡迎交流指導。 一、路由基礎 1.路由配置 & 顯示路由組件的view(類比angular的ui-view) 路由配置:路由匹配的規則 view:放置路由組件的地方(URL匹配了, ...


前言:

  本文為個人學習react-router的總結。包括路由基礎配置,跳轉,許可權管理,組件與路由配置的關係,代碼分割。歡迎交流指導。

一、路由基礎

  1.路由配置 & 顯示路由組件的view(類比angular的ui-view)

  路由配置:路由匹配的規則

render((
    <Router history={ hashHistory }>
        <Route path="/" component={ App }>
            <Route path="select" component={ Select }></Route>
            <Route path="found" component={ Found }></Route>
            <Route path="follow" component={ Follow }></Route>
            <Route path="my" component={ My }>
                <Route path=":myname" component={ MyName }></Route>
                <Route path="mysex" component={ MySex }></Route>
            </Route>
        </Route>
    </Router>
), document.getElementById('root'));

  view:放置路由組件的地方(URL匹配了,然後對應的組件應該放到什麼地方去),

  每一個Route都只是一個組件,子路由就是 this.props.children 裡面的組件,Route通過匹配URL決定顯示哪一個子路由

class App extends PureComponent {
    render() {
        return (
            <div>
                <GlobalNav />
                { this.props.children } { /* this.props.children 是被嵌套在App的組件,相當於放子路由的View*/}
            </div>
        )
    }
}

 

二、預設路由(IndexRoute )

  組件<App /> 的匹配路徑是 ‘/', 有四個子路由,當前路由只是'/',那麼<App />應該顯示什麼頁面呢?

  這裡給與IndexRoute組件 -- 若希望直接使用4個其中一個則使用IndexRedirect

render((
    <Router history={ hashHistory }>
        <Route path="/" component={ App }>
            <IndexRoute component={ IndexApp } />
            <Route path="select" component={ Select }></Route>
            <Route path="found" component={ Found }></Route>
            <Route path="follow" component={ Follow }></Route>
            <Route path="my" component={ My }>
                <Route path=":myname" component={ MyName }></Route>
                <Route path="mysex" component={ MySex }></Route>
            </Route>
        </Route>
    </Router>
), document.getElementById('root'));

 

   如果不使用IndexRoute組件,也還有一種投機取巧的方法,直接在 App組件中,使用  {this.props.children || <IndexApp />} ,在ui展示的層面上修改this.props.children為undefined的情況。

   缺點:這種形式,沒有參與到路由機制中,onEnter,onLeave 等HOOK都無法使用

三、路由跳轉   

  1. IndexLink & Link (active狀態之爭)

   倘若有如下兩個鏈接,正好URL是'/my/mioawwwww', 兩個路由都匹配的了,那麼就是兩個都是active狀態(相應地添加activeStyle,activeClassName的樣式)

<Link to="/my" >Mypage</Link>
<Link to="/my/:myname" >myname</Link>

 

  若你只想為 <Link to="/my/:myname" >myname</Link> 這一個按鈕添加active樣式,就可以為 <Link to="/my" >Mypage</Link> 使用IndexLink

<IndexLink to="/my" >Mypage</IndexLink>
<Link to="/my/:myname" >myname</Link>

  IndexLink是補充Link的,只要URL完整匹配'/my'的時候才會激活active狀態

  2.跳轉參數 to

     2.1:通過 to=’xx/xx' 直接跳轉 <Link to={`/my/${myname}/info`}>check my info</Link> 

     2.2:to=對象,帶參數跳轉(pathname, query, hash, state(額外數據)),註意:這些參數都被存放到this.props.location中

 <li><Link to={{pathname:"/select", hash:'#ahash', query:{foo: 'bar', boo:'boz'}, state:{data:'miao'}  }} activeClassName="GlobalNav-active">精選</Link></li>

     2.3:to=函數,註冊到路由跳轉事件中,每一次路由變化,都會執行該函數,並經最新的location作為參數

<Link to={location => ({ ...location, query: { name: 'ryan' } })}>
  Hello
</Link>

    2.4:不使用Link,在函數內直接操作router

      舊版本:由於router只用的context傳遞路由信息,因此每一個組件都可以輕易的通過this.context.router獲取路由

      新版本:router被放置在this.props中,通過this.props.router可以獲取路由

      註意:push與replace的區別,一個是添加,一個是替換,歷史記錄中被替換的已經不存在了,所以瀏覽器回退不到替換前的頁面。

    changeRouter = () => {
        console.log(this.props)
        // this.props.router.push('/follow');
        // this.props.router.push({
        //     pathname:'/follow',
        //     state:{name:'xxx'},
        //     query: {foo: 'bar'}
        // })
        
        // this.props.router.replace('/follow');
        this.props.router.replace({
            pathname: '/follow',
            query: {foo:'bar'}
        })
    }

 

四、重定向

  <Redirect>:重定向到同等級的其他路由

   <Redirect from="name/xxx" to='mysex' /> 

render((
    <Router history={ browserHistory }>
        <Route path="/" component={ App }>
            <IndexRoute component={ IndexApp } />
            <Route path="select" component={ Select }></Route>
            <Route path="found" component={ Found } onEnter={onEnterHook} onLeave={onLeaveHook}></Route>
            <Route path="follow" component={ Follow }>
            </Route>
            <Route path="my" component={ My } >
                <Redirect from="name/xxx" to='mysex' />
                
                <Route path="name/:myname" component={ MyName }>
                    <Route path="info" component={ MyInfo } ></Route>
                </Route>
                <Route path="mysex" component={ MySex } />
            </Route>
            <Redirect from="*" to='/' />
        </Route>
    </Router>
), document.getElementById('root'));

 

  <IndexRedirect>:從父路由的目錄開始重定向

<Route path="/" component={App}>
  <IndexRedirect to="/welcome" />
  <Route path="welcome" component={Welcome} />
  <Route path="about" component={About} />
</Route>

 

 

五、路由機制的許可權

  1.onEnter

const onEnterHook = (nextState, replace /*,cb*//*若添加cb參數,鉤子變成非同步執行,cb返回之前,將發生阻塞*/) => {
    console.log('onenter', nextState);
    // replace // 是router.replace(),若訪問者沒有許可權,則引導到其他頁面
}

  nextState的屬性

  2.onLeave:與onEnter類似,nextState屬性不同

   3.onChange(prevState, nextState, replace, callback?) ,用於子路由,

    進入該路由的某個子路由是觸發,或者改變query,hash

    一旦添加onChange屬性,則子路由通過onChangeHook決定,Link不起作用

   

 六、組件與路由的一一對應關係,按需載入組件

<Route path="follow" component={ Follow }></Route> // this.props.children;
<Route path="follow" component={ {main:Follow, sidebar: Sidebar} }></Route> // const { main, sidebar } = this.props;

 

   非同步載入組件,使用(需要加上 require.ensure([], (require) => {}) 實現代碼分割

   getComponent(nextState, callback)  &&  getComponents(nextState, callback) 

   cb(err, component) 

  getComponent(nextState, cb) {
    require.ensure([], (require) => {
      cb(null, require('./components/Calendar'))
    })
  }

 

   

 七、每一個Route組件的屬性

 

 

 八、另一種路由配置的方式

const selectRouter = {
    path:'select',
    component: Select
}
const foundRouter = {
    path:'found',
    component: Found
}
const myRouter = {
    path:'my',
    getComponent(nextState,cb) {
        cb(null, My)
    }
}
// import Follow from './components/Follow.js';
const followRouter = {
    path:'follow',
    getComponent(nextState,cb) {
        require.ensure([], (require) => {
            cb(null, require('./components/Follow'))
        })
    }
    // getComponent(nextState, cb) {
    //     cb(null, Follow)
    // }
}
const rootRouter = {
    path: '/',
    component: App,
    // indexRoute: {component:IndexApp},
    childRoutes: [
        selectRouter,
        foundRouter,
        followRouter,
        // require('./components/Follow.index'),
        myRouter
        ]
}
// const rootRouter = {
//     path: '/',
//     component: App,
//     getIndexRoute(partialNextState, cb) {
//         cb(null,  {compoment:IndexApp});
//     },
//     getChildRoutes(location, cb) {
//         cb(null, [
//             selectRouter,
//             foundRouter,
//             followRouter,
//             myRouter
//         ])
//     }
// }
render(
    <Router history={browserHistory} routes={rootRouter} />,
    document.getElementById('root')
)

 

   代碼分割的註意事項:

    1. require.ensure中分割的組件,需要使用module.export 暴露出來

module.exports = xxx; //可獲取xxx組件
export default xxx // 不可獲取xxx組件 

     2. getComponent,getComponents,getIndexRoute,getChildRoutes只是實現了非同步載入,要實現代碼分割還是要使用require.ensure

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 層次選擇器: $("div li")獲取div下的所有li元素(後代、子、子的子......) $("div>li")獲取div下的直接li子元素。 $(".menuitem+div")獲取樣式名為menuitem之後的第一個div元素(不常用)。 $(".menuitem~div")獲取樣式名為m ...
  • yield next和yield* next之間到底有什麼區別?為什麼需要yield* next?經常會有人提出這個問題。雖然我們在代碼中會儘量避免使用yield* next以減少新用戶的疑惑,但還是經常會有人問到這個問題。為了體現自由,我們在koa框架內部使用了yield* next,但是為了避免 ...
  • 2017年也已經開始了快一個月的時間了,然而我卻好像沒有跨進新一年的感覺 過去的一年裡,前端開發這個行業的變化給我的感覺實在是快得有點眼花繚亂應接不暇 各種工程化工具,各種新框架占據了這個行業裡人們日常討論的話題 然而反思回自己的團隊,仍然是停留在比較基礎的開發流程。不得不讓我恍然醒悟,自己真是半年 ...
  • 在JS中,引擎,編譯器,作用域分別扮演以下角色: 引擎:負責整個Js程式的編譯以及執行過程。 編譯器:負責語法分析以及代碼生成等。 作用域:負責收集並維護所有聲明的標示符(變數)組成的一系列查詢,並實施一套嚴格的規則,確定當前執行的代碼對這些標識符的訪問許可權。 下麵用一個小例子來表示: var a ...
  • new document 首頁 列表 內容 聯繫 關於 ...
  • ...
  • Tabslet Yet another jQuery plugin for tabs, lightweight, easy to use and with some extra features Demonstration page Documentation (wiki) 實例DEMO 運行一下 ...
  • 特性說明和原理圖: 標準瀏覽器和Ie9+瀏覽器都支持事件的冒泡和捕獲,而IE8-瀏覽器只支持冒泡 標準和Ie9+瀏覽器用stopPropagation()或cancelBubble阻止事件傳播,而ie8-用e.cancelBubble屬性來阻冒泡,註意ie9不支持cancelBubble屬性(設置後 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...