我正在尝试通过以下示例来使路由器动态路由生效:https://github.com/rackt/react-router/tree/master/examples/huge-apps
这是我的设置:
webpack.config.js
:
module.exports = {
devtool: 'inline-source-map',
entry: './js/app.js',
output: {
path: '../public',
filename: 'test-app.js',
chunkFilename: '[id].chunk.js',
publicPath: '/public/'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: [
'es2015',
'react'
]
}
}
]
}
./js/app.js
:
import React from 'react';
import ReactDOM from 'react-dom';
import { createHistory, useBasename } from 'history';
import { Router } from 'react-router';
const history = useBasename(createHistory)({
basename: '/'
});
const rootRoute = {
component: 'div',
childRoutes: [{
path: '/',
component: require('./components/App')
}]
};
ReactDOM.render(
<Router history={history} routes={rootRoute} />,
document.getElementById('root')
);
./components/App.js
:
import React from 'react';
console.log('testing');
export default class App extends React.Component {
render() {
console.log('App');
return (
<div>
App!
</div>
)
}
}
index.html
:
<!doctype html>
<html>
<head>
<title>Test App</title>
</head>
<body>
Hello
<div id="root"></div>
<script src="public/test-app.js"></script>
</body>
</html>
当我运行服务器时,我看到index.html
显示 Hello ,我也看到来自console.log('testing')
的{{1}},但实际App.js
组件不呈现。有什么想法吗?
谢谢!
修改
如果我将App.js
更改为下面的ES5语法,它就可以了!这是为什么?路由器./components/App.js
的反应是否与ES6不兼容?
component: require('./components/App')
答案 0 :(得分:5)
我认为,您使用的是Babel 6,它们changed commonjs require
语法。
现在,您需要添加default
:
component: require('./components/App').default
我遇到了同样的问题,终于找到了如何使它发挥作用。