dva subscription的使用方法

import { routerRedux } from 'dva/router'
export default {
  namespace: 'notice',
  state: {
    notices:[],
    loading: false,
    editModalVisible: false
  },
  effects: {
    *watchAndRefreshList({ dispatch }, { put, call, take }){
      let listAction = {};
    //关闭弹框后重新获取数据
    //调用hideModal就会触发fetch
while(true){ const action = yield take(['notice/fetch', 'notice/hideModal']); console.log('action', action); if(action.type == 'notice/fetch'){ action.type = 'fetch'; listAction = action; } if(action.type == 'notice/hideModal'){ action.type = 'hideModal'; dispatch(listAction); } } }, *fetch({ payload }, { call, put }) { const response = yield call(Get, '/api/notices'); yield put({ type: 'save', payload: response }) }, *gologin({ payload }, { call, put }) { yield put(routerRedux.push('/user/login')) } }, reducers: { save(state, action) { return { ...state, notices: action.payload } }, showModal(state, action){ return { ...state, editModalVisible: true } }, hideModal(state, action){ return { ...state, editModalVisible: false, } }, }, subscriptions: { //监听地址,如果地址含有app则跳转到登陆页 setup({ dispatch, history }) { history.listen(location => { if (location.pathname.includes('app')) { dispatch({ type: 'gologin' }) } }); }, watchAndRefreshList({ dispatch, history }){ dispatch({ type: 'watchAndRefreshList', dispatch }); } }, };

 

转载于:https://www.cnblogs.com/AnnieShen/p/8028231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值