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

react-router-dom之異步加載路由方式

 更新時(shí)間:2023年03月24日 14:14:21   作者:養(yǎng)只貓  
這篇文章主要介紹了react-router-dom之異步加載路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

react-router-dom異步加載路由

這篇文章跟http://www.dbjr.com.cn/article/278887.htm銜接

在一個(gè)spa單頁(yè)面應(yīng)用中如果項(xiàng)目較小的話(huà)異步組件可能影響不大,但是如果是一個(gè)大的react單頁(yè)面項(xiàng)目如果沒(méi)有使用異步組件,頁(yè)面會(huì)在第一次加載的時(shí)候加載所有項(xiàng)目中所有的組件嚴(yán)重影響頁(yè)面的加載速度,異步組件可以讓路由跳轉(zhuǎn)到對(duì)應(yīng)的路由上才去加載對(duì)應(yīng)的react文件,這樣頁(yè)面的加載速度就會(huì)得到很大的提升。因此異步組件是非常有必要的。

先來(lái)看看異步組件是怎么寫(xiě)的吧

安裝依賴(lài)react-loadable

npm install react-loadable

新建一個(gè)js文件。異步組件也是要依賴(lài)于react所以要引入react。

意思大概就是在loader引入組件完成前先顯示“正在加載”

//入口文件中不在調(diào)用之前的RouterTest組件而是調(diào)用這個(gè)異步組件
//獲取路由參數(shù)的方法需要進(jìn)行調(diào)整要在對(duì)應(yīng)的再組件中調(diào)用react-router-dom中的withRouter方法
//然后export default connect(null,null)(withRouter(RouterTest))
import React from 'react'
import Loadable from 'react-loadable'
const LoadingRouterTest = Loadable({
	loader:() => import('./RouterTest.js'),
	loading () {return <div>正在加載</div>}
})
 
export default () => <LoadingRouterTest />

既然用了異步組件其他幾個(gè)地方就要做相應(yīng)的改動(dòng)。首先是入口文件

我們將RouterTest替換成LoadingRouterTest這個(gè)組件

<Route path="/LoadingRouterTest:id" component={LoadingRouterTest}></Route>
          <Route path="/RouterTest:id" component={RouterTest}></Route>

我們跳轉(zhuǎn)也是跳轉(zhuǎn)LoadingRouterTest這個(gè)路由url變成這樣,但是頁(yè)面還是RouterTest。

這里會(huì)發(fā)現(xiàn)一個(gè)問(wèn)題就是路由的參數(shù)獲取不到了

原因是因?yàn)閡rl名稱(chēng)變了但是頁(yè)面還是RouterTest

這里需要用到react-router-dom的withRouter

在暴露RouterTest組件的時(shí)候使用withRouter這個(gè)方法允許RouterTest使用LoadingRouterTest的url參數(shù)

export default connect(null,null)(withRouter(RouterTest))

個(gè)人認(rèn)為react-router-dom相對(duì)vue-router還是比較復(fù)雜的不管是傳參還是異步組件,但是可操作的空間還是有的。

react路由組件異步加載,優(yōu)化白屏

//手寫(xiě)異步加載高階組件
import React, { Component } from "react";
 
export default function asyncComponent(importComponent) {
  class AsyncComponent extends Component {
    constructor(props) {
      super(props);
 
      this.state = {
        component: null
      };
    }
 
    async componentDidMount() {
      const { default: component } = await importComponent();
 
      this.setState({component});
    }
 
    render() {
      const C = this.state.component;
 
      return C ? <C {...this.props} /> : null;
    }
  }
 
  return AsyncComponent;
}
import React, { Component } from 'react';
import { HashRouter, Switch, Route, Redirect } from 'react-router-dom';
import asyncComponent from '@/utils/asyncComponent';
 
import home from "@/pages/home/home";
const record = asyncComponent(() => import("@/pages/record/record"));
const helpcenter = asyncComponent(() => import("@/pages/helpcenter/helpcenter"));
const production = asyncComponent(() => import("@/pages/production/production"));
const balance = asyncComponent(() => import("@/pages/balance/balance"));
 
// react-router4 不再推薦將所有路由規(guī)則放在同一個(gè)地方集中式路由,子路由應(yīng)該由父組件動(dòng)態(tài)配置,組件在哪里匹配就在哪里渲染,更加靈活
export default class RouteConfig extends Component{
  render(){
    return(
      <HashRouter>
        <Switch>
          <Route path="/" exact component={home} />
          <Route path="/record" component={record} />
          <Route path="/helpcenter" component={helpcenter} />
          <Route path="/production" component={production} />
          <Route path="/balance" component={balance} />
          <Redirect to="/" />
        </Switch>
      </HashRouter>
    )
  }
}
 /**注解

   * 路由表中:

   * const record = asyncComponent(() => import("@/pages/record/record"));

   * <Route path="/record" component={record} />

   * --------------------------------------------------------------

   * >>1

   * asyncComponent函數(shù)返回了一個(gè)組件AsyncComponent,AsyncComponent被record變量接收;

   * asyncComponent函數(shù)參數(shù)是一個(gè)()=>import('xxx')方法,該方法是用來(lái)引入文件的,返回的的是一個(gè)promise對(duì)象。

   * >>2

   * 當(dāng)react路由匹配到對(duì)應(yīng)路徑,AsyncComponent組件掛在,參數(shù)()=>import('xxx')方法在其掛在后執(zhí)行,將異步加載的組件更改到狀態(tài),并再次重新渲染。

  */

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • React 全面解析excel文件

    React 全面解析excel文件

    這篇文章主要介紹了React 全面解析excel文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • react將文件轉(zhuǎn)為base64上傳的示例代碼

    react將文件轉(zhuǎn)為base64上傳的示例代碼

    本文主要介紹了react將文件轉(zhuǎn)為base64上傳的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • useReducer使用詳解及其應(yīng)用場(chǎng)景

    useReducer使用詳解及其應(yīng)用場(chǎng)景

    這篇文章主要介紹了useReducer使用詳解及其應(yīng)用場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React useCallback使用教程

    React useCallback使用教程

    useCallback是react中比較重要的一個(gè)hook,useCallback 用來(lái)返回一個(gè)函數(shù),在父子組件傳參或者通用函數(shù)封裝中,起到舉足輕重的作用
    2023-01-01
  • 關(guān)于react中的常見(jiàn)錯(cuò)誤及解決

    關(guān)于react中的常見(jiàn)錯(cuò)誤及解決

    這篇文章主要介紹了關(guān)于react中的常見(jiàn)錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 使用 React Router Dom 實(shí)現(xiàn)路由導(dǎo)航的詳細(xì)過(guò)程

    使用 React Router Dom 實(shí)現(xiàn)路由導(dǎo)航的詳細(xì)過(guò)程

    React Router Dom 是 React 應(yīng)用程序中用于處理路由的常用庫(kù),它提供了一系列組件和 API 來(lái)管理應(yīng)用程序的路由,這篇文章主要介紹了使用 React Router Dom 實(shí)現(xiàn)路由導(dǎo)航,需要的朋友可以參考下
    2024-03-03
  • React路由攔截模式及withRouter示例詳解

    React路由攔截模式及withRouter示例詳解

    這篇文章主要為大家介紹了React路由攔截模式及withRouter示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • react清空ant.design中表單內(nèi)容的方法實(shí)現(xiàn)

    react清空ant.design中表單內(nèi)容的方法實(shí)現(xiàn)

    本文主要介紹了react清空ant.design中表單內(nèi)容的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • React實(shí)現(xiàn)下拉框的key,value的值同時(shí)傳送

    React實(shí)現(xiàn)下拉框的key,value的值同時(shí)傳送

    這篇文章主要介紹了React實(shí)現(xiàn)下拉框的key,value的值同時(shí)傳送方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • React根據(jù)寬度自適應(yīng)高度的示例代碼

    React根據(jù)寬度自適應(yīng)高度的示例代碼

    本篇文章主要介紹了React根據(jù)寬度自適應(yīng)高度的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10

最新評(píng)論