在构造函数中定义路由

时间:2017-01-27 19:09:14

标签: javascript reactjs ecmascript-6 react-router

我有一个名为index.js的组件,它为我的const store = configureStore(); ReactDOM.render( <Root store={store} />, document.getElementById('root'), );

呈现路由
Root

我需要执行此操作,因为我将reduxonEnter连接起来,这是由redux的root授权所致,并且需要使用class Root extends React.Component { constructor(props) { super(props); // some stuff } requireAuthentication = (nextState, replace) => { // some stuff for managing auth }; render() { const { store } = this.props; return ( <div> <Provider store={store}> <Router history={browserHistory}> <Route path="/" component={App}> <Route component={Dashboard}> <IndexRoute component={Home} onEnter={requireAuthentication}/> </Route> <Route path="login" component={Login} /> </Route> </Router> </Provider> </div> ); } } // some definitions for connecting Root to redux export default connect(mapStateToProps, mapDispatchToProps)(Root); 状态。

所以这是根:

Warning: [react-router] You cannot change <Router routes>; it will be ignored

好的......这很好,但我有一个警告:

routes

路线,也很好......但我想解决警告。所以...谷歌搜索我发现问题是一个可能的解决方案:在构造函数中声明Router并通过props将其传递给import React, { PropTypes } from 'react'; import { connect, Provider } from 'react-redux'; import { bindActionCreators } from 'redux'; import { Router, browserHistory, Route, IndexRoute } from 'react-router'; import { loginActions } from '../actions'; import { App, Dashboard, Home, Login } from '../features'; import adalHandler from '../services/adal'; class Root extends React.Component { constructor(props) { super(props); const routes = ( <Route path="/" component={App}> <Route component={Dashboard}> <IndexRoute component={Home} /> </Route> <Route path="login" component={Login} /> </Route> ); // some stuff } } requireAuthentication = (nextState, replace) => { // stuff }; render() { const { store } = this.props; return ( <div> <Provider store={store}> <Router history={browserHistory} routes={this.routes} /> </Provider> </div> ); } } // stuff export default connect(mapStateToProps, mapDispatchToProps)(Root);

我的构造函数我这样做了:

me.el

但是现在,它不能很好地渲染。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

对象SELECT *, (CASE Kg WHEN 0 THEN 1 ELSE Kg END) * Qty * Rate AS Total FROM Products 未在render方法中定义。

例如,您可以使用this.routes替换构造函数中的const routes = (...);,以便在render方法中访问对象。