【发布时间】:2020-01-03 11:55:44
【问题描述】:
在我的 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) 返回之前访问过的页面值。
【问题讨论】:
标签: reactjs react-redux react-router