如何使用react路由器处理带有lazyloaded组件的面包屑
How do you handle breadcrumbs with lazyloaded components using react router?
我一直在寻找一个关于如何为延迟加载的路由处理面包屑的示例。我已经能够在服务器上正常工作,但还没有能够在客户端上获得由getComponent()屏蔽的延迟加载组件。如何访问异步组件?
路由.js
module.exports = {
component: Layout,
childRoutes: [
{
path: '/',
component: App,
childRoutes: [
{
path: '/lazy',
component: Lazy, // Lazy loaded component
}
]
}
]
};
Lazy.js
// Server polyfill
if (typeof require.ensure !== 'function') { require.ensure = function(d, c) { c(require) }; }
module.exports = {
path: 'detail',
getComponent: function(location, cb) {
require.ensure([], (require) => {
cb(null, require('./components/Detail')); //Component I want to access
});
}
};
面包屑.js
import React, { Component, PropTypes } from 'react';
class Breadcrumbs extends Component {
static propTypes = {
routes: PropTypes.array.isRequired
};
render() {
const { routes } = this.props;
return (
<ol className="breadcrumbs">
{routes.map((route, index) => {
return (
<li key={index}>{route.component.title}</li>
);
})}
</ol>
);
}
}
export default Breadcrumbs;
问题是异步路由看起来像:
{
path: 'detail',
component: {
getComponent: function()
}
}
代替:
{
path: 'detail',
component: {
title: 'Lazy Detail'
}
}
我只想把title
放在路由对象上,而不是组件上,这样可以解决您的问题,并给您更多的灵活性。
如果必须这样做,您可以使用自定义RoutingContext
,但这是一个半私有的API,可能会在v1.1.x中消失。不过,一般来说,在<Route>
上定义它似乎更干净。
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- 为react组件传递道具的最佳方式
- [Vue warn]:未能解析组件
- 如何控制组件'的createContent函数被激发
- react组件中的绑定方法
- 如何在react js中移动第二个组件
- 多个组件是如何实现的
- 组件生命周期问题/无法处理未定义的问题
- 如何使用react路由器处理带有lazyloaded组件的面包屑