From fe954092a4d34613ce57d5d319ce2355997a33ba Mon Sep 17 00:00:00 2001 From: zuiidea Date: Sat, 6 Jul 2019 10:50:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20Fixing=20dva=20history.listen?= =?UTF-8?q?=20multiple=20triggers.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- src/pages/user/index.js | 23 +++++++++-------------- src/pages/user/model.js | 15 ++++++++++++++- src/plugins/onError.js | 9 +++++++-- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index d5f07f4..0cfa207 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "d3-shape": "^1.3.0", "draftjs-to-html": "^0.8.4", "draftjs-to-markdown": "^0.5.1", + "dva": "2.4.1", "dva-model-extend": "^0.1.2", "echarts": "^4.2.0", "echarts-for-react": "^2.0.15-beta.0", @@ -63,7 +64,7 @@ "stylelint": "^10.0.0", "stylelint-config-prettier": "^5.2.0", "stylelint-config-standard": "^18.3.0", - "umi": "^2.8.7", + "umi": "^2.8.9", "umi-plugin-react": "^1.9.6" }, "lint-staged": { diff --git a/src/pages/user/index.js b/src/pages/user/index.js index e996a05..f510fe6 100644 --- a/src/pages/user/index.js +++ b/src/pages/user/index.js @@ -13,24 +13,19 @@ import Modal from './components/Modal' @withI18n() @connect(({ user, loading }) => ({ user, loading })) class User extends PureComponent { - componentDidMount() { - this.handleRefresh({ page: 1, pageSize: 10 }) - } - handleRefresh = newQuery => { - const { location, dispatch } = this.props + const { location } = this.props const { query, pathname } = location - const payload = { - ...query, - ...newQuery, - } - dispatch({ - type: 'user/query', - payload, - }) + router.push({ pathname, - search: stringify(payload, { arrayFormat: 'repeat' }), + search: stringify( + { + ...query, + ...newQuery, + }, + { arrayFormat: 'repeat' } + ), }) } diff --git a/src/pages/user/model.js b/src/pages/user/model.js index d774c8b..e598c07 100644 --- a/src/pages/user/model.js +++ b/src/pages/user/model.js @@ -1,4 +1,3 @@ -/* global window */ import modelExtend from 'dva-model-extend' import { pathMatchRegexp } from 'utils' import api from 'api' @@ -22,6 +21,20 @@ export default modelExtend(pageModel, { selectedRowKeys: [], }, + subscriptions: { + setup({ dispatch, history }) { + history.listen(location => { + if (pathMatchRegexp('/user', location.pathname)) { + const payload = location.query || { page: 1, pageSize: 10 } + dispatch({ + type: 'query', + payload, + }) + } + }) + }, + }, + effects: { *query({ payload = {} }, { call, put }) { const data = yield call(queryUserList, payload) diff --git a/src/plugins/onError.js b/src/plugins/onError.js index f300c0a..d929ab9 100644 --- a/src/plugins/onError.js +++ b/src/plugins/onError.js @@ -1,8 +1,13 @@ import { message } from 'antd' export default { - onError(e) { + onError(e, a) { e.preventDefault() - message.error(e.message) + if (e.message) { + message.error(e.message) + } else { + /* eslint-disable */ + console.error(e) + } }, }