You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import React,{Component} from 'react';
import ReactDom from 'react-dom';
export default class A extends Component{
render(){
return <div>
this is A
</div>
}
}
ReactDom.render(<A />,document.getElementById('component'))
步骤四: 集成react-loadable
现在用react-loadable方式�实现的按需加载是官方比较推荐的方式。
import React,{Component} from 'react';
import { BrowserRouter as Router, Route, Switch,Link } from 'react-router-dom';
import ReactDom from 'react-dom';
import Loadable from 'react-loadable';
const Loading = () => <div>Loading...</div>;
const B = Loadable({
loader: () => import('./b.js'),
loading: Loading,
})
const C = Loadable({
loader: () => import('./C.js'),
loading: Loading,
})
export default class A extends Component{
render(){
return <div>
<Router>
<div>
<Route path="/B" component={B}/>
<Route path="/C" component={C}/>
<Link to="/B">to B</Link><br/>
<Link to="/C">to C</Link>
</div>
</Router>
</div>
}
}
ReactDom.render(<A/>,document.getElementById('component'))
按需加载
如果系统很庞大,将代码一次性载入,就显得太过于强大,最好能做到根据我们的需求来选择性地加载我们需要的代码。
webpack 提供了2种方式来拆分代码。
现在都比较推荐第一种方式,从实践中可以得到下面的结论
在业务中,如果使用的是
react
,一般都是需要和react-router
联合使用的。那么一般配置如下,
步骤一
webpack.config.js
步骤二: 添加.babelrc
步骤三:书写jsx
步骤四: 集成react-loadable
现在用
react-loadable
方式�实现的按需加载是官方比较推荐的方式。步骤五:封装
Loadable
组件如果用上面的方式来封装组件,会发现报错。因为webpack编译的时候,会先扫描一遍js文件,找出需要按需加载的部分,然后进行按需加载,不会关心内部的js执行上下文。
所以只能把import的部分拿出来。
脑阔疼的webpack按需加载
The text was updated successfully, but these errors were encountered: