单击Swal取消按钮时重定向,但在Swal之外单击时不重定向



我尝试过使用result.dismissresult.isDismissedresult.isDenied,但似乎没有足够的具体性来检查用户是否在swal消息之外单击。这是我的代码:

Swal.fire({
position: "top-end",
title: `${name} successfully added to cart!`,
icon: "success",
showCancelButton: true,
cancelButtonText: "Continue shopping",
cancelButtonColor: "green",
confirmButtonText: "Go To Cart",
confirmButtonColor: "blue",
}).then((result) => {
if (result.isConfirmed) history.push("/cart");
else if (location.pathname == "/" && !result.dismiss)
history.push("/products");
});

谢谢!

您可以通过检查结果.dismiss原因是否等于Swal.DismissReason.cancel:来检查是否使用取消按钮解除警报

Swal.fire({
position: "top-end",
title: "Item successfully added to cart!",
icon: "success",
showCancelButton: true,
cancelButtonText: "Continue shopping",
cancelButtonColor: "green",
confirmButtonText: "Go To Cart",
confirmButtonColor: "blue",
}).then((result) => {
if (result.dismiss === Swal.DismissReason.cancel) {
console.log("Redirect for cancel");
} else {
console.log("Redirect for non-cancel dismissals");
}
});
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.16/dist/sweetalert2.all.min.js"></script>

最新更新