国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 12:18:53
字體:
供稿:網(wǎng)友

前言

相信很多人都遇到過想在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)異步加載。

// 異步按需加載componentfunction 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的這個問題,原因是當(dāng)離開頁面以后,組件已經(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌什县| 江源县| 龙井市| 华坪县| 长子县| 兴和县| 阜南县| 苗栗县| 阿拉善左旗| 平凉市| 镇江市| 阿拉善右旗| 铜川市| 海口市| 丁青县| 栾川县| 郯城县| 友谊县| 博乐市| 荃湾区| 花莲县| 广元市| 中江县| 达拉特旗| 徐州市| 周宁县| 赤水市| 大宁县| 思南县| 黄骅市| 上饶市| 炎陵县| 车险| 房产| 涞水县| 仁寿县| 乐都县| 巧家县| 米林县| 九龙城区| 新民市|