ReactJS:onClick`window.location.href`返回上一页值
在我的react项目中,我需要根据页面位置对redux操作设置一个条件 所以我使用了ReactJS:onClick`window.location.href`返回上一页值,reactjs,react-redux,react-router,Reactjs,React Redux,React Router,在我的react项目中,我需要根据页面位置对redux操作设置一个条件 所以我使用了window.location.href来获取当前页面的值,但它返回上次访问的页面值 export const getBlog = (type, offset) => { console.log(window); console.log(window.location.pathname); if( window.location.pathname !== '/planner' ){ re
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(窗口)
屏幕截图;返回实际访问的页面位置结果
第二个console.log(window.location.pathname)
返回以前访问过的页面值
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'})
});
}
}
}
window.location.pathname
始终提供您登录的页面。由于我们正在使用react router
获取当前路由,因此您需要使用this.props.location
为了更清楚,你可以参考这个答案
window.location.pathname
始终提供您登录的页面。由于我们正在使用react router
获取当前路由,因此您需要使用this.props.location
为了更清楚,你可以参考这个答案
我相信当您登录时,控制台不会对窗口进行深度复制。 展开“位置”节点时,将加载该值
这就解释了为什么这两个结果不同。我相信,当您记录窗口时,控制台不会对其进行深度复制。 展开“位置”节点时,将加载该值
这就解释了为什么这两个结果不同。在这个redux函数中使用
this.props.location
时,它返回我一个props
未定义的类型错误。在这个redux函数中使用this.props.location
时,它返回我一个props
未定义的类型错误。