0

我已尝试遵循本指南此问题,但是在ReactRouter.match触发时,所有回调参数都未定义。

这是我的服务器:

var app = new (require('express'))()
var port = 3000

var ReactDomServer = require('react-dom/server');
var ReactRouter = require('react-router');
var routes = require('./es6/routes.js');

app.use(function(req, res) {
    console.log(req.url);
    ReactRouter.match({ routes: routes, location: req.url }, function(error, redirectLocation, renderProps) {
        console.log('error', error); // undefined
        console.log('erredirectLocationror', redirectLocation); // undefined
        console.log('renderProps', renderProps); // undefined
    });
})

app.listen(port, function(error) {
  if (error) {
    console.error(error)
  } else {
    console.info("==> Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port)
  }
})

就像评论显示的一样,,,,error并且redirectLocation所有renderProps日志都为undefined

记录routes变量开始:

{ default: 
   { '$$typeof': Symbol(react.element),
     type: 
      { [Function]
        displayName: 'Route',
        createRouteFromReactElement: [Function: createRouteFromReactElement],
        propTypes: [Object] },
     key: null,
     ref: null,
     props: { path: '/', component: [Function: App], children: [Object] },
     _owner: null,
     _store: {} } }

它以打字稿文件routes.tsx开始:

import * as React from 'react'
import { Route } from 'react-router'
import App from './containers/app';
import Home from './containers/home';

export default (
    <Route path='/' component={App}>
        <Route path='/home' component={Home} />
    </Route>
)

编译为es6/routes.js

"use strict";
const React = require('react');
const react_router_1 = require('react-router');
const app_1 = require('./containers/app');
const home_1 = require('./containers/home');
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (React.createElement(react_router_1.Route, {path: '/', component: app_1.default}, React.createElement(react_router_1.Route, {path: '/home', component: home_1.default})));
//# sourceMappingURL=routes.js.map

我错过了什么?

4

1 回答 1

1

您正在获得undefined价值,因为没有任何东西匹配,它是 404ing。

没有匹配的原因是因为您混合了 es5 和 es6 模块。尝试传递routes.default给 match()。

于 2016-06-07T19:09:20.263 回答