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

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

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

前言

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

引入必要的依賴

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

接下來(lái)創(chuàng)建一個(gè)component函數(shù)

目的就是為了變?yōu)閞outer的component實(shí)現(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(){ 
    //重寫(xiě)組件的setState方法,直接返回空
    this.setState = (state,callback)=>{
      return;
    }; 
  }
   render() {
    const { Component } = this.state
    if (Component) {
     return <Component {...this.props} />
    }
    return null
   }
  }
 }

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

接下來(lái)實(shí)現(xiàn)本地文件路徑的傳入

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

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

<Router history={hashHistory}>
    <Route name="home" breadcrumbName="首頁(yè)" path="/" component={MainLayout}>
      <IndexRoute name="undefined" breadcrumbName="未定義" component={() => <div>未定義</div>}/>
      <Route name="Development" breadcrumbName="施工中" path="Development" component={DevelopmentPage}/>
      <Route breadcrumbName="個(gè)人助理" path="CustomerWorkTodo" component={({children}) => <div className="box">{children}</div>}>
        <Route name="Agency" breadcrumbName="待辦事項(xiàng)" path="Agency" component={asyncComponent(() => load('GlobalNotification/CustomerWorkAssistantTodo/CustomerAgencyMatter'))}/>
        <Route name="Already" breadcrumbName="已辦事項(xiàng)" 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)實(shí)現(xiàn)了router 的component 的引入,這樣自然就可以通過(guò)一個(gè)循環(huán)來(lái)實(shí)現(xiàn)動(dòng)態(tài)的加載啦!

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

相關(guān)文章

  • React DnD如何處理拖拽詳解

    React DnD如何處理拖拽詳解

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

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

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

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

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

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

    這篇文章主要介紹了關(guān)于antd tree 和父子組件之間的傳值問(wèn)題,是小編給大家總結(jié)的一些react知識(shí)點(diǎn),本文通過(guò)一個(gè)項(xiàng)目需求實(shí)例代碼詳解給大家介紹的非常詳細(xì),需要的朋友可以參考下
    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解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 一看就懂的ReactJs基礎(chǔ)入門教程-精華版

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

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

    react配置代理setupProxy.js無(wú)法訪問(wèn)v3.0版本問(wèn)題

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

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

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

    React插槽使用方法

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

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

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

最新評(píng)論