dva的subscriptions監聽id變化后路由跳轉


例如 /AAA/id的頁面

AAA變化時路由跳轉很好寫,當只點擊其id文章變化后,地址欄id變化了,但頁面沒有刷新變化,所以加一個監聽函數subscriptions    

首先npm install path-to-regexp

然后在該列表頁AAA里的models層找到接受數據頁
import pathToRegexp from 'path-to-regexp';
 
在effects后寫上
subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        const path = pathToRegexp('/AAA/:id').exec(pathname);
        if (pathname === '/AAA') {
          dispatch({
            type: 'fetchList',//這里的fetchlist就是你effects里請求接口獲取數據的函數名
            payload: {},
          });
        }
        if (path) {
          dispatch({
            type: 'content/fetchDetail',//這里的content/fetchDetail就是該id頁內models層對應內容頁里effects里請求接口獲取數據的函數名
            payload: path[1],
          });
        }
      });
    },
  },
     
以上寫完后,其他該怎么寫還是正常寫,不變,只是加上這段就能監聽id變化了
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM