Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dva1.x 和 dva2.x 中 subscriptions 的history.listen获取路由参数 #2

Open
SunShinewyf opened this issue Feb 28, 2018 · 0 comments

Comments

@SunShinewyf
Copy link
Owner

在 dva1.x 中,组件的 的监听函数写法如下:

  function pageChangeHandler(page) {
    dispatch(routerRedux.push({
      pathname: '/users',
      query: { page },
    }));
  }

然后在 model 中的监听中可以如下面形式进行获取:

 subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/users') {
          dispatch({ type: 'fetch', payload: query });
        }
      });
    },
  },

但是在2.x中,history的 location 属性上不再包含 query, 需要 query-string 处理一遍。并且要把参数传到 location 中的 search 字段中,例子如下:

 function pageChangeHandle(page) {
    dispatch(
      routerRedux.push({
        pathname: "/user",
        search: "?page=" + page
      })
    );
  }

model 中的监听如下:

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, search }) => {
        const query = queryString.parse(history.location.search)
        if (pathname === "/user") {
          dispatch({
            type: "fetch",
            payload: query ? query : 1
          });
        }
      });
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant