react-router / redux-simple路由器加载深度路由

时间:2016-01-20 14:03:53

标签: javascript react-router react-router-redux

我正在努力使用react-router(使用redux-simple-router,fwiw)应该是一件简单的事情。

我有一些基本路线定义如下:

<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的redner方法具有处理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 '<'错误,好像它不期望index.html加载webpack js。我可以导航到/我很好,这是一种没有意义的东西。

我错过了什么简单的方法才能使其适用于直接和间接导航?谢谢!

2 个答案:

答案 0 :(得分:0)

好吧,我最终弄明白了,它与我一直在思考的事情没什么关系。事实证明,我的getScheduledReports()方法是假设将填充redux状态,以及导航到报告/它不是马上的时候。

从redux存储添加状态对象的null / undefined检查,并在不存在时返回null,为我修复它。一旦商店填满并且报告可用,我的组件就会看到它并刷新。

答案 1 :(得分:0)

您可以在react-router中进行深度路由,如下所示:

<Route path="/" component={Layout} >
    <IndexRoute component={Homepage} />
    <Route path="home" component={Homepage} />
    <Route path="bayaans" component={Bayaans} />
    <Route path="read-quran" component={Readquran} />
    <Route path="duas" component={Duas} />
    <Route path="events" component={Events} />
    <Route path="contact" component={Contactpage} />
</Route>

<Route path="/admin" component={AdminLayout} >
    <IndexRoute component={LoginPg} />
    <Route path="dashboard" component={Dashboard} />
    <Route path="list-bayaans" component={AdminBayaansPg} />
    <Route path="list-duas" component={AdminDuasPg} />
    <Route path="list-events" component={AdminEventsPg} />
</Route>