webpack and react-router按需加载
原理:CommonJS与import()
方法一:CommonJS模块语法
利用require.ensure,require.ensure()是webpack特有的,已经被import()取代。
方法
require.ensure( dependencies: String[], callback: function(require), errorCallback: function(error), chunkName: String )
方法二:import()
ES2015 loader规范定义了import()方法,可以在运行时动态地加载ES2015模块
方法
import('Component').then() // or 在 async中使用 await import('Component')
demo
import React, { Component } from 'react'; class App extends Component { handleClick = () => { import('./moduleA') .then(({ moduleA }) => { // Use moduleA }) .catch(err => { // Handle failure }); }; render() { return ( <div> <button onClick={this.handleClick}>Load</button> </div> ); } } export default App;
react-router中使用按需加载
demo地址,此处配合create-react-app使用,自己配置webpack合理需要配置output.fileName和output.chunkFilename
方法一:使用react.lazy
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import React, { Suspense, lazy } from 'react'; const Program1 = lazy(() => import('./Program1')); const App = () => ( <Router> <Suspense fallback={<div>Loading...</div>}> <Switch> <Route path="/program1" component={Program1}/> </Switch> </Suspense> </Router> );
方法二:利用高阶组件
- 写一个高阶组件用于动态加载组件
// async Component 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: component }); } render() { const C = this.state.component; return C ? <C {...this.props} /> : null; } } return AsyncComponent; }
- 引用并使用该高阶组件做按需加载
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import asyncComponent from './asyncComponent'; import React, { Suspense } from 'react'; const Progran2 = asyncComponent(() => import("./Program2")); const App = () => ( <Router> <Suspense fallback={<div>Loading...</div>}> <Switch> <Route exact path="/program2" component={Program2}/> </Switch> </Suspense> </Router> );
以上两种方法都是react官方推荐
code-splitting
相关推荐
不知道该写啥QAQ 2020-08-02
不知道该写啥QAQ 2020-06-10
不知道该写啥QAQ 2020-11-12
webfullStack 2020-11-09
Yvettre 2020-09-15
想做大牛的蜗牛 2020-10-30
gloria0 2020-10-26
gaojie0 2020-09-11
SelinaChan 2020-08-14
不知道该写啥QAQ 2020-08-09
gloria0 2020-08-09
hline 2020-07-29
SelinaChan 2020-07-28
wangdianyong 2020-07-23
webpackvuees 2020-07-23
yqoxygen 2020-07-20
不知道该写啥QAQ 2020-07-18
waterv 2020-07-18