簡體   English   中英

ReactJS:onClick`window.location.href`返回上一頁值

[英]ReactJS: onClick `window.location.href` return previous page value

在我的React項目中,我需要根據頁面位置來確定redux操作的條件。

所以我已經使用window.location.href來獲取當前頁面的值,但是它返回了上次訪問的頁面值。

export const getBlog = (type, offset) => {
  console.log(window);
  console.log(window.location.pathname);
  if( window.location.pathname !== '/planner' ){
    return (dispatch) => {

        dispatch({ type: 'BLOG_LOADING_START'})

      axios.get(siteurl + TPW.CATEGORY_API + type, {
        headers: { 'Content-Type': 'application/json' } })
        .then(response => {
        let ary =  response.data.slice(0, offset)
        dispatch({
            type: 'SET_BLOG_DATA',
            list: response.data,
            blogData: ary,
            total: response.data.length,
            offSetCnt: 3
        })
        dispatch({ type: 'BLOG_LOADING_DONE'})
      });
    }
  }
}

首先console.log(window)屏幕截圖; 返回實際訪問頁面的位置結果。

在此處輸入圖片說明

第二個console.log(window.location.pathname)返回前一次訪問的頁面值。

在此處輸入圖片說明

window.location.pathname始終為您提供登陸的頁面。 由於我們使用react-router來獲取當前路由,因此您需要使用this.props.location

您可以參考此答案以更清楚

獲取位置

我相信控制台在您登錄時不會對窗口進行深層復制。 展開位置節點時,它將加載值。

這就解釋了為什么這兩個結果不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM