【发布时间】:2015-05-11 21:12:47
【问题描述】:
我正在处理一个 React-Router 测试存根示例,我以前从未见过这种类约定。我猜这是 ES6 特有的?
var stubRouterContext = (Component, props, stubs) => {
function RouterStub() { }
Object.assign(RouterStub, {
makePath () {},
makeHref () {},
transitionTo () {},
replaceWith () {},
goBack () {},
getCurrentPath () {},
getCurrentRoutes () {},
getCurrentPathname () {},
getCurrentParams () {},
getCurrentQuery () {},
isActive () {},
getRouteAtDepth() {},
setRouteComponentAtDepth() {}
}, stubs);
return React.createClass({
childContextTypes: {
router: React.PropTypes.func,
routeDepth: React.PropTypes.number
},
getChildContext () {
return {
router: RouterStub,
routeDepth: 0
};
},
render () {
return <Component {...props} />
}
});
};
为什么我会得到 TypeError: object is not function when I do this?
var Subject = stubRouterContext(Handler, props);
这里是文档https://github.com/rackt/react-router/blob/master/docs/guides/testing.md的链接
【问题讨论】:
-
是的,是ES6箭头函数语法,看这里github.com/lukehoban/es6features
-
你在编译你的代码吗?怎么样?
-
没关系,这是重复的。 @JMM,我正在使用 babel,但我只是忘记导出模块。关闭。谢谢。
标签: javascript reactjs react-router