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

關(guān)于React動態(tài)加載路由處理的相關(guān)問題

 更新時間:2019年01月07日 14:53:56   作者:王振寧  
這篇文章主要介紹了關(guān)于React動態(tài)加載路由處理的相關(guān)問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

相信很多人都遇到過想在React項目中動態(tài)加載路由這種問題,接下來我們逐步實現(xiàn)。

引入必要的依賴

import React from 'react'
import { Router, Route, IndexRoute, hashHistory } from 'react-router'

接下來創(chuàng)建一個component函數(shù)

目的就是為了變?yōu)閞outer的component實現(xiàn)異步加載。

// 異步按需加載component
function asyncComponent(getComponent) {
  return class AsyncComponent extends React.Component {
   static Component = null;
   state = { Component: AsyncComponent.Component };
 
   componentDidMount() {
    if (!this.state.Component) {
     getComponent().then(({default: Component}) => {
      AsyncComponent.Component = Component
      this.setState({ Component })
     })
    }
   }
   //組件將被卸載 
  componentWillUnmount(){ 
    //重寫組件的setState方法,直接返回空
    this.setState = (state,callback)=>{
      return;
    }; 
  }
   render() {
    const { Component } = this.state
    if (Component) {
     return <Component {...this.props} />
    }
    return null
   }
  }
 }

在此說明componentWillUnmount鉤子是為了解決Can only update a mounted or mounting component的這個問題,原因是當離開頁面以后,組件已經(jīng)被卸載,執(zhí)行setState時無法找到渲染組件。

接下來實現(xiàn)本地文件路徑的傳入

 function load(component) {
  return import(`./routes/${component}`)
 }

將已知地址路徑傳遞到一個函數(shù)并把這個函數(shù)作為參數(shù)傳遞到 asyncComponent中這樣asyncComponent就能接收到這個路由的地址了,然后我們要做的就是將這個asyncComponent函數(shù)帶入到router中。

<Router history={hashHistory}>
    <Route name="home" breadcrumbName="首頁" path="/" component={MainLayout}>
      <IndexRoute name="undefined" breadcrumbName="未定義" component={() => <div>未定義</div>}/>
      <Route name="Development" breadcrumbName="施工中" path="Development" component={DevelopmentPage}/>
      <Route breadcrumbName="個人助理" path="CustomerWorkTodo" component={({children}) => <div className="box">{children}</div>}>
        <Route name="Agency" breadcrumbName="待辦事項" path="Agency" component={asyncComponent(() => load('GlobalNotification/CustomerWorkAssistantTodo/CustomerAgencyMatter'))}/>
        <Route name="Already" breadcrumbName="已辦事項" path="Already" component={asyncComponent(() => load('GlobalNotification/CustomerWorkAssistantTodo/CustomerAlreadyMatter'))}/>
        <Route name="SystemMessage" breadcrumbName="系統(tǒng)消息" path="SystemMessage/:data" component={asyncComponent(() => load('GlobalNotification/SystemMessage/SystemMessage'))}/>
        <Route name="SystemMessagePer" breadcrumbName="系統(tǒng)消息詳情" path="SystemMessagePer/:data" component={asyncComponent(() => load('GlobalNotification/SystemMessage/SystemMessagePer'))}/>
      </Route>
    </Router>
 </Router>    

從代碼中可以看出已經(jīng)實現(xiàn)了router 的component 的引入,這樣自然就可以通過一個循環(huán)來實現(xiàn)動態(tài)的加載啦!

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

相關(guān)文章

  • React DnD如何處理拖拽詳解

    React DnD如何處理拖拽詳解

    這篇文章主要為大家介紹了React DnD如何處理拖拽示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • react如何獲取URL中參數(shù)

    react如何獲取URL中參數(shù)

    這篇文章主要介紹了react如何獲取URL中參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 詳解react-native WebView 返回處理(非回調(diào)方法可解決)

    詳解react-native WebView 返回處理(非回調(diào)方法可解決)

    這篇文章主要介紹了詳解react-native WebView 返回處理(非回調(diào)方法可解決),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 關(guān)于antd tree和父子組件之間的傳值問題(react 總結(jié))

    關(guān)于antd tree和父子組件之間的傳值問題(react 總結(jié))

    這篇文章主要介紹了關(guān)于antd tree 和父子組件之間的傳值問題,是小編給大家總結(jié)的一些react知識點,本文通過一個項目需求實例代碼詳解給大家介紹的非常詳細,需要的朋友可以參考下
    2021-06-06
  • React?SSR架構(gòu)Stream?Rendering與Suspense?for?Data?Fetching

    React?SSR架構(gòu)Stream?Rendering與Suspense?for?Data?Fetching

    這篇文章主要為大家介紹了React?SSR架構(gòu)Stream?Rendering與Suspense?for?Data?Fetching解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 一看就懂的ReactJs基礎(chǔ)入門教程-精華版

    一看就懂的ReactJs基礎(chǔ)入門教程-精華版

    現(xiàn)在最熱門的前端框架有AngularJS、React、Bootstrap等。自從接觸了ReactJS,ReactJs的虛擬DOM(Virtual DOM)和組件化的開發(fā)深深的吸引了我,下面來跟我一起領(lǐng)略ReactJs的風(fēng)采吧~~ 文章有點長,耐心讀完,你會有很大收獲哦
    2021-04-04
  • react配置代理setupProxy.js無法訪問v3.0版本問題

    react配置代理setupProxy.js無法訪問v3.0版本問題

    這篇文章主要介紹了react配置代理setupProxy.js無法訪問v3.0版本問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • React特征學(xué)習(xí)Form數(shù)據(jù)管理示例詳解

    React特征學(xué)習(xí)Form數(shù)據(jù)管理示例詳解

    這篇文章主要為大家介紹了React特征學(xué)習(xí)Form數(shù)據(jù)管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • React插槽使用方法

    React插槽使用方法

    本文主要介紹了React插槽使用方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • react中使用better-scroll滾動插件的實現(xiàn)示例

    react中使用better-scroll滾動插件的實現(xiàn)示例

    滾動在很多地方都可以使用,本文主要介紹了react中使用better-scroll滾動插件的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論