我正在努力使用react-router(與redux-simple-router,fwiw)應該是一件簡單的事情。react-router/redux-simple路由器加載深度路由
我有一些基本的路由定義像這樣:
<Route path="/" component={AppRoot}>
<Route path="all" component={ScheduledReportList} mine={false}/>
<Route path="mine" component={ScheduledReportList} mine={true}/>
<Route path="report/:name" component={ScheduledReportList} mine={false}/>
</Route>
ScheduledReportList
的瑞德納方法來處理mine
參數的設施,和邏輯來處理name
支柱的存在(或缺乏)(剪切不相關的部分)。
render() {
const { name } = this.props.params;
if (name != undefined && name != null) {
// we are displaying details for a single report only
let values = getScheduledReports();
let currentReport = _.find(values, (report) => {
return report.name == name;
});
if (this.props.route.mine) {
return (
<MyReportDetails... />
);
} else {
return (
<ReportDetails... />
);
}
} else {
// we are displaying all reports
<snip...>
values.map((report, i) => {
let anchor = undefined;
if (this.props.route.mine) {
anchor = <a onClick={() => {this.props.routeActions.replace('/myreport/' + report.name)}}>{report.name}</a>
} else {
anchor = <a onClick={() => {this.props.routeActions.replace('/report/' + report.name)}}>{report.name}</a>
}
<snip...>
我的問題是這樣的:客戶端我可以很好地導航。使用稱爲routeActions.replace()
或routeActions.push()
的錨點或跨度,那麼一切都很好。這個問題特別是當我處於「深度」路徑(即,其中有一個斜線:/reports/fooReport
與/all
),我刷新頁面,或者如果我嘗試直接導航到它。試圖加載/reports/foo
頁面服務器端給我一個SyntaxError: expected expression, got '<'
錯誤,就好像它不期望加載webpack js的index.html。我可以導航到/我很好,這是沒有道理的。
我錯過了什麼簡單的事情,以獲得這對直接和間接導航工作?謝謝!
看看你的開發工具的網絡面板。哪個請求出錯?我的猜測是,您正在從不匹配的頁面提供index.html,並且index.html具有一個腳本標記,其中包含由webpack生成的包的相對url。你的index.html是什麼樣的? – ibash
那麼,看一些調試日誌確實揭示了一些:App:Server/report/SomeReport + 0ms App:Server /report/report-server-react.js + 0ms所以我修復了index.html中的相對路徑,它得到了這個錯誤消失,但通過REDX狀態似乎並沒有傳播到現在的深層頁面... – snowell
你使用瀏覽器歷史API? https://github.com/rackt/react-router/blob/master/docs/guides/basics/Histories.md#browserhistory – ibash