标签:有一个 心得 部分 getc 支持 component 工具 port 方式
const routes = { path: ‘/‘, component: AppCom, indexRoute: { component: Admin }, childRoutes: [ { path: ‘edit/:id‘, getComponent: (nextState, cb) => { require.ensure([], (require) => { cb(null, require(‘../entry/admin/edit.jsx‘)) }) } } ] } <Router routes={routes} history={hashHistory} />
import React from ‘react‘; export default class Bundle extends React.Component { constructor(props) { super(props); this.state = { mod: null }; } componentWillMount() { this.load(this.props) } componentWillReceiveProps(nextProps) { if (nextProps.load !== this.props.load) { this.load(nextProps) } } load(props) { this.setState({ mod: null }); props.load().then((mod) => { this.setState({ mod: mod.default ? mod.default : mod }); }); } render() { return this.state.mod ? this.props.children(this.state.mod) : null; } }
import Bundle from ‘./Bundle‘; const routeObj = { url1:‘url1/url1‘, url2:‘url2/url2‘, url3:‘url3/url3‘, } let components = {}; for(let route in routeObj){ components[route] = (props) => ( <Bundle load={() => import(`../entry/${routeObj[route]}`)}> {(Component) => <Component {...props}/>} </Bundle> ); }
import(`../entry/${routeObj[route]}`)}
<Route exact path="/" component={components.url1}/>
output: { path: paths.appBuild, filename: ‘[name].bundle.js‘, chunkFilename: ‘[name].bundle.js‘ }
标签:有一个 心得 部分 getc 支持 component 工具 port 方式
原文地址:https://www.cnblogs.com/coderhero/p/9210720.html