在注销时,我试图从浏览器中删除令牌cookie,但使用当前的代码似乎不起作用。此外,当浏览器中仍然可见cookie时,Cookie.get()函数将返回一个空对象。我试过寻找其他解决方案,但似乎没有任何效果。
import { useNavigate } from "react-router-dom";
import history from "../../history";
import Cookie from "js-cookie";
export default function Tool() {
const navigate = useNavigate();
function logout() {
localStorage.clear();
Cookie.remove("token");
console.log(Cookie.get(), "Oooooooooooooo");
navigate("/login");
}
return (
<div >
<div className="topButtons">
<button onClick={() => logout()}>LogOut</button>
</div>
</div>
);
}发布于 2022-09-21 21:12:08
我也经历过类似的行为,并发现了this,这可能是因为一个bug,可以通过在cookie选项参数中指定path来解决。这为我解决了这个问题。
有了js-cookie你就会这样做:
// set the cookie
Cookie.set('token', 'token.example.value', {path: '/'});
// remove the cookie
Cookie.remove('token', {path: '/'});发布于 2022-07-24 18:20:21
先尝试删除所有cookie:document.cookie = ''
发布于 2022-07-24 18:25:46
使用从库中删除的方法
Cookies.remove('name')
// Removing a nonexistent cookie neither raises any exception nor returns any value.
https://stackoverflow.com/questions/73100935
复制相似问题