最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解webpack + react + react-router 如何實(shí)現(xiàn)懶加載

 更新時(shí)間:2017年11月20日 11:15:47   作者:M.M.F  
這篇文章主要介紹了詳解webpack + react + react-router 如何實(shí)現(xiàn)懶加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

在 Webpack 1 中主要是由bundle-loader進(jìn)行懶加載,而 Webpack 2 中引入了類似于 SystemJS 的System.import語法,首先我們對于System.import的執(zhí)行流程進(jìn)行簡單闡述:

  1. Webpack 會(huì)在編譯過程中掃描代碼庫時(shí)將發(fā)現(xiàn)的System.import調(diào)用引入的文件及其相關(guān)依賴進(jìn)行單獨(dú)打包,注意,Webpack 會(huì)保證這些獨(dú)立模塊及其依賴不會(huì)與主應(yīng)用的包體相沖突。
  2. 當(dāng)我們訪問到這些獨(dú)立打包的組件模塊時(shí),Webpack 會(huì)發(fā)起 JSONP 請求來抓取相關(guān)的包體。
  3. System.import 同樣也是 Promise,在請求完成之后System.import會(huì)將抓取到的模塊作為參數(shù)傳入then中的回調(diào)函數(shù)。
  4. 如果我們重復(fù)訪問已經(jīng)加載完畢的模塊,Webpack 不會(huì)重復(fù)執(zhí)行抓取與解析的過程。

而 React Router 路由的懶加載實(shí)際上分為動(dòng)態(tài)路由與與懶加載兩步,典型的所謂動(dòng)態(tài)路由配置如下:

/**
 * <Route path="/" component={Core}>
 *  <IndexRoute component={Home}/>
 *  <Route path="about" component={About}/>
 *  <Route path="users" component={Users}>
 *  <Route path="*" component={Home}/>
 * </Route>
 */
export default {
 path: '/', 
 component: Core,
 indexRoute: { 
  getComponent(location, cb) {
    ...
  },
 },
 childRoutes: [
  {
   path: 'about', 
   getComponent(location, cb) {
    ...
   },
  },
  {
   path: 'users', 
   getComponent(location, cb) {
    ...
   },
  },
  {
   path: '*', 
   getComponent(location, cb) {
    ...
   },
  },
 ],
};

正常打包

import IndexPage from './views/app.jsx'
import AboutPage from './views/about.jsx'
export default function({history}) {
  return (
    <Router history={history}>
      <Route path="/" component={IndexPage} />
      <Route path="/about" component={AboutPage} />
    </Router>
  )
}

這是一個(gè)正常打包的路由寫法, 如果需要分割代碼, 我們需要改造下路由, 借助getComponent和require.ensure

webpack 代碼分割

export default function({history}) {
  return (
    <Router history={history}>
      <Route path="/" getComponent={(location, callback) => {
        require.ensure([], function(require) {
          callback(null, require('./HomePage.jsx'))
        })
      }} />
      <Route path="/about" getComponent={(location, callback) => {
        require.ensure([], function(require) {
          callback(null, require('./AboutPage.jsx'))
        })
      }} />
    </Router>
  )
}

這樣看來代碼有點(diǎn)累, 我們稍微改造下

const home = (location, callback) => {
 require.ensure([], require => {
  callback(null, require('./HomePage.jsx'))
 }, 'home')
}

const about = (location, callback) => {
 require.ensure([], require => {
  callback(null, require('./AboutPage.jsx'))
 }, 'about')
}
export default function({history}) {
  return (
    <Router history={history}>
      <Route path="/" getComponent={home}></Route>
      <Route path="/about" getComponent={about}></Route>
    </Router>
  )
}

這樣看起來是不是簡潔了很多

注意: 由于webpack的原因, 如果直接require('./AboutPage.jsx')不能正常加載, 請嘗試require('./AboutPage.jsx').default

webpack2 代碼分割

上面的代碼看起來好像都是webpack1的寫法, 那么webpack2呢?

webpac2就需要借助System.import了

export default function({history}) {
  return (
    <Router history={history}>
      <Route path="/" getComponent={(location, callback) => {
        System.import('./HomePage.jsx').then(component => {
          callback(null, component.default || component)
        })
      }} />
      <Route path="/about" getComponent={(location, callback) => {
        System.import('./AboutPage.jsx').then(component => {
          callback(null, component.default || component)
        })
      }} />
    </Router>
  )
}

我們一樣可以把上面的代碼優(yōu)化一下

const home = (location, callback) => {
  System.import('./HomePage.jsx').then(component => {
    callback(null, component.default || component)
  })
}
const about = (location, callback) => {
  System.import('./AboutPage.jsx').then(component => {
    callback(null, component.default || component)
  })
}

export default ({ history }) => {
  return (
    <Router history={history}>
      <Route name="home" path="/" getComponent={home} />
      <Route name="about" path="/about" getComponent={about} />
    </Router>
  )
}

webpack2 + dva 實(shí)現(xiàn)路由和 models 懶加載

const routerThen = (app, callback, [component, model]) => {
  app.model(model.default || model)
  callback(null, component.default || component)
}

export default ({ history, app }) => {
  return (
    <Router history={history}>
      <Route name="home" path="/" getComponent={(location, callback) => {
        Promise.all([
          System.import('./views/app.jsx'),
          System.import('./models/topics')
        ]).then(routerThen.bind(null, app, callback))
      }} />
      <Route name="article" path="/article/:id" getComponent={(location, callback) => {
        Promise.all([
          System.import('./views/article.jsx'),
          System.import('./models/topic')
        ]).then(routerThen.bind(null, app, callback))
      }} />
    </Router>
  )
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React渲染機(jī)制超詳細(xì)講解

    React渲染機(jī)制超詳細(xì)講解

    React整個(gè)的渲染機(jī)制就是React會(huì)調(diào)用render()函數(shù)構(gòu)建一棵Dom樹,在state/props發(fā)生改變的時(shí)候,render()函數(shù)會(huì)被再次調(diào)用渲染出另外一棵樹,重新渲染所有的節(jié)點(diǎn),構(gòu)造出新的虛擬Dom tree
    2022-11-11
  • React+valtio響應(yīng)式狀態(tài)管理

    React+valtio響應(yīng)式狀態(tài)管理

    Valtio是一個(gè)很輕量級(jí)的響應(yīng)式狀態(tài)管理庫,使用外部狀態(tài)代理去驅(qū)動(dòng)React視圖來更新,本文主要介紹了React+valtio響應(yīng)式狀態(tài)管理,感興趣的可以了解一下
    2023-12-12
  • React18系列commit從0實(shí)現(xiàn)源碼解析

    React18系列commit從0實(shí)現(xiàn)源碼解析

    這篇文章主要為大家介紹了React18系列commit從0實(shí)現(xiàn)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • React前端開發(fā)createElement源碼解讀

    React前端開發(fā)createElement源碼解讀

    這篇文章主要為大家介紹了React前端開發(fā)createElement源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • React復(fù)制到剪貼板的示例代碼

    React復(fù)制到剪貼板的示例代碼

    本篇文章主要介紹了React復(fù)制到剪貼板的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • React中useRef與useState的使用與區(qū)別

    React中useRef與useState的使用與區(qū)別

    本文介紹了React中兩個(gè)常用的鉤子useRef和useState,包含比較它們的功能并提供示例來說明它們的用法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-11-11
  • react實(shí)現(xiàn)antd線上主題動(dòng)態(tài)切換功能

    react實(shí)現(xiàn)antd線上主題動(dòng)態(tài)切換功能

    這篇文章主要介紹了react實(shí)現(xiàn)antd線上主題動(dòng)態(tài)切換功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • React組件渲染后對DOM的操作方式

    React組件渲染后對DOM的操作方式

    這篇文章主要介紹了React組件渲染后對DOM的操作方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 搭建React?Native熱更新平臺(tái)的詳細(xì)過程

    搭建React?Native熱更新平臺(tái)的詳細(xì)過程

    這篇文章主要介紹了搭建React?Native熱更新平臺(tái),要實(shí)現(xiàn)?React?Native?熱更新功能,有四種思路?Code?Push、Pushy、Expo?和自研,感興趣的朋友一起通過本文學(xué)習(xí)吧
    2022-05-05
  • React父子組件互相通信的實(shí)現(xiàn)示例

    React父子組件互相通信的實(shí)現(xiàn)示例

    React中是單向數(shù)據(jù)流,數(shù)據(jù)只能從父組件通過屬性的方式傳給其子組件,本文主要介紹了React父子組件互相通信的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11

最新評(píng)論

田林县| 屏东县| 信宜市| 红原县| 金堂县| 建瓯市| 景洪市| 呼和浩特市| 平度市| 广南县| 新宾| 岳阳市| 高雄市| 延寿县| 肥城市| 富源县| 贡觉县| 富源县| 乐陵市| 大余县| 项城市| 黑河市| 扎赉特旗| 长宁县| 宜都市| 大荔县| 荔浦县| 芦溪县| 福安市| 河北省| 安多县| 华亭县| 旌德县| 尼玛县| 南充市| 新乡县| 治多县| 莱州市| 贺州市| 滨海县| 泰安市|