2
这是一个简单的站点,利用react-router进行“代码拆分”。我在这里上传了源代码:https://github.com/wangjia184/log_viewer子路由加载但未呈现
在app.js中,定义了以下路由。
const rootRoute = {
component: 'div',
childRoutes: [ {
path: '/',
component: require('./routes/setting/view'),
childRoutes: [
require('./routes/indices')
]
}]
};
在routes/setting/view,存在将用户重定向的链接/indices
class SettingUI extends React.Component {
render() {
return <Link to={`/indices`} activeClassName="active">Go to /indices</Link>
}
};
当我点击这个链接,在地址栏中的URL改变了,getComponent
内routes/indices/index.js方法被触发。
module.exports = {
path: 'indices',
getComponent(location, cb) {
require.ensure([], (require) => {
cb(null, require('./view'));
})
}
}
class IndicesUI extends React.Component {
componentWillMount() {
console.log('componentWillMount()');
}
componentDidMount() {
console.log('componentDidMount()');
}
componentWillUnmount() {
console.log('componentWillUnmount()');
}
render() {
console.log('render()');
return <h2>IndicesUI</h2>
}
}
console.log('IndicesUI is loaded');
module.exports = IndicesUI;
但实际上,组件已加载但未呈现。网页浏览器控制台中没有错误,但显示没有更新。
目录/build
包含由webpack生成的最终文件。
我很欣赏任何人看着这个。
修订:
const loadContainerAsync = bundle => (location, cb) => {
bundle(component => {
cb(null, component);
});
};
ReactDOM.render((
<Router history={hashHistory}>
<Route path='/' component={DefaultView}>
<Route path="indices" getComponent={loadContainerAsync(require('bundle?lazy!./components/indices'))} />
</Route>
</Router>
), document.getElementById('app-container'));
是的,静态路由的工作,但是这不是我想要的。我想要的是儿童路线的延迟加载。我是基于https://github.com/rackt/react-router/tree/master/examples/huge-apps –
的示例编码谢谢,也许我没有清楚地解释这个问题。我期望组件被延迟加载,直到用户被重定向到那里。我更新了问题并找到了解决方案。 –