我跟着一对夫妇的例子,试图摆脱路由接入参数在处理它作出react的组成部分。然而,console.log
在orthis.props
内部的结果总是在我希望它包含来自路由的时候。render
componentDidMount
{}
gameId
gamesView
client.js
启动路由器:
// HTML5 History API fix for local
if (config.environment === 'dev') {
var router = Router.create({ routes: routes });
} else {
var router = Router.create({ routes: routes, location: Router.HistoryLocation });
}
router.run(function(Handler) {
React.render(
<Handler />,
document.getElementById('app')
);
});
routes.js
为简单起见,删除了一些路线:
var routes = (
<Route name='home' path='/' handler={app}>
<DefaultRoute handler={home} location="/" />
<Route name='gamesView' path='/games/:gameId' handler={gamesView} />
</Route>
);
module.exports = routes;
...并且app.js
包装了其他路线,我{...this.props}
在RouteHandler
. 如果我console.log(this.props)
从render
这里的函数内部也返回{}
:
var App = React.createClass({
render: function() {
return (
<div className='container'>
<div className='row'>
<RouteHandler {...this.props} />
</div>
</div>
);
}
});
module.exports = App;
最后gamesView
是我希望看到 props 对象的React 组件。这this.props
也是{}
,以下导致错误TypeError: $__0 is undefined var $__0= this.props.params,gameId=$__0.gameId;
:
var GamesView = React.createClass({
render: function() {
var { gameId } = this.props.params;
return (
<div>
<h1>Game Name</h1>
<p>{gameId}</p>
</div>
);
}
});
module.exports = GamesView;
有人有任何想法吗?