0

我们在fluxible-router中有两条路由。我需要如果访问 legacyDek 路线,那么在完成操作后,它将重定向到甲板路线。

deck: {
        path: '/deck/:id(\\d+|\\d+-\\d+)/:stype?/:sid?/:spath?/:mode?/:theme?',
        method: 'get',
        page: 'deck',
        handler: require('../components/Deck/Deck'),
        action: (context, payload, done) => {
            async.series([
                (callback) => {
                    context.executeAction(loadDeck, payload, callback);
                },
                (callback) => {
                    context.executeAction(loadPresentation, payload, callback);
                }
            ],
            (err) => {
                if(err) console.log(err);
                done();
            });
        }
    },
legacydeck: {
        path: '/deck/:oldid(\\d+_\\w+.*)',
        method: 'get',
        action: (context, payload, done) => {
            context.executeAction(loadLegacy, payload, (err, result) => {
                if (err) console.log(err);
                context.executeAction(navigateAction, {'url' : '/deck/' +result}, done);
            });
        }
    },

重定向本身确实可以正常工作,但是 url 行保持不变。有人可以解释一下 - 为什么会这样以及如何完全重定向?

4

1 回答 1

0

目前唯一的解决方案如下: 1. 更改触发重定向的操作,使其返回错误对象,在我的示例中:

export default function loadLegacy(context, payload, done) {
    log.info(context);
    context.service.read('deck.legacy', {'oldid': parseInt(payload.params.oldid)}, {timeout: 20 * 1000}, (err, res) => {
      //  console.log('Executing loadPresentation action');
        if (err) {
            log.error(context, {filepath: __filename});
            context.executeAction(serviceUnavailable, payload, done);
            //context.dispatch('LOAD_FEATURED_FAILURE', err);
        } else {
            done({'statusCode':'301','redirectURL': '/deck/' + res.new_id});
        }
    });
}

  1. 更改 server.js 文件,添加行以捕获“301”错误。

context.getActionContext().executeAction(navigateAction, {url: req.url, reqId: req.reqId}, 
  (err) => {
      if (err) {         
          if (err.statusCode && err.statusCode === '301') {  //handling redirect             
              res.redirect(err.redirectURL);
          }else{ //handling other errors          
          ...
          }
      }else{  //usual rendering     
      ...
      }
})

  1. 路线现在如下所示:

legacydeck: {
        path: '/deck/:oldid(\\d+_\\w+.*)',
        method: 'get',
        action: (context, payload, done) => {
            context.executeAction(loadLegacy, payload, done);
        }
    },

于 2017-07-13T15:09:53.953 回答