在我的WebPack,應用程序的入口點設置爲index.js
:路由但不能從反應JS反應路由器
entry: {
app: [
'react-hot-loader/patch',
'./client/index.js'
]
在節點JS,對於路徑/
在我的申請,我路由到index.html
從routes.js
使用服務器端:
app.route('/*')
.get((req, res) => {
res.sendFile(path.resolve(`${app.get('appPath')}/index.html`));
});
上面的代碼可以理解提供的index.html。
但是如果我想讓我的應用程序路由使用React-router
進行初始化呢?
index.html得到渲染,但我沒有看到index.js
得到初始化所有。它被定義爲webpack中的入口點,所以應該發生,對嗎?
問題:要有陣營路由初始化前應該工作,一旦流量到達index.js
我routes.js
客戶端文件夾中看起來是這樣的:
import React from 'react';
import {Route, IndexRoute} from 'react-router';
import About from './components/About/About';
import Header from './components/Header/Header';
import Outlet from './components/Home/SelectOutlet';
console.log("routes file client")
export default (
<Route path="/" component={Header}>
<IndexRoute component={Outlet}/>
<Route path="about" component={Footer}/>
</Route>
);
和index.js
import 'babel-polyfill'; //certain functions like array.from , set, map can't be transpiled by babel so ue poyfill for that
import React from 'react';
import { render } from 'react-dom';
import { Router, browserHistory } from 'react-router';
import routes from './routes';
//import '../node_modules/bootstrap/dist/css/bootstrap.min.css'; //webpack can import CSS files too
import './styles/styles.css';
render((
<Router history={browserHistory} routes={routes}/>),
document.getElementById('app')
);
console.log("In index js render ")
控制檯index.js
和routes.js
永遠不會得到安慰和應用程序只是起到index.html
因爲快遞路由
我的WebPack文件就是從這裏取:https://github.com/Hashnode/mern-starter
,但我沒有看到bundle.js
得到與NPM啓動命令創建任意位置。
編輯: 錯誤截圖: 畫面1: error message
當我點擊此錯誤消息:
我得到的所有的HTML中JS內容:
<!DOCTYPE html>
<html>
<head>
<title>
ABC
</title>
</head>
<body>
<h1> XYZ</h1>
<div id="app"></div>
<script src="index.js"></script>
</body>
</html>
是的,我試過了。我有意想不到的符號<!從index.js第一行的控制檯中的index.html。當我在控制檯中點擊這個文件時,它會顯示'index.js',並顯示我在index.html中的任何內容。 @kevin – gags
您一定需要在index.html中包含該腳本,但您需要修復該錯誤。你能對你的錯誤更具體嗎?或者顯示它的屏幕截圖 – Kevin
嘿,當然,更新了問題 – gags