Dva: 请问,在request.js里无法做路由跳转吗?

Created on 3 Oct 2016  ·  2Comments  ·  Source: dvajs/dva

下面进入401仍然无效

import fetch from 'dva/fetch';
import {routerRedux} from 'dva/router';
function parseJSON(response) {
  return response.json();
}

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }

  //hava no jurisdiction
  if(response.status === 401) {
    routerRedux.push('/login');
    return response;
  }


  const error = new Error(response.statusText);
  error.response = response;
  throw error;
}

有没有好的处理方法?我如果不做判断,throw error在index.js里也捕获不到异常啊!谢谢!1

Most helpful comment

request 里 reject { type: 'redirect', path: '' },然后在 effect 里 try catch,捕获到 redirect 的 type,put routerRedux.push 的 action 进行跳转。

这看起来不是最好的方式,[email protected] 会在 onError 里支持 dispatch,这样就能全局捕获,然后进行跳转了。

All 2 comments

@longzb routerRedux.push/replace等等都是一个动作描述,没法做具体跳转,需要使用dispatch进行跳转

request 里 reject { type: 'redirect', path: '' },然后在 effect 里 try catch,捕获到 redirect 的 type,put routerRedux.push 的 action 进行跳转。

这看起来不是最好的方式,[email protected] 会在 onError 里支持 dispatch,这样就能全局捕获,然后进行跳转了。

Was this page helpful?
0 / 5 - 0 ratings