简体   繁体   English

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

[英]Redirect when clicked on Swal cancel button but not when clicked outside of Swal

I've tried to use result.dismiss , result.isDismissed , and result.isDenied but I can't seem to get specific enough to check if the user clicked outside of the swal message.我尝试使用result.dismissresult.isDismissedresult.isDenied ,但我似乎无法足够具体地检查用户是否在 swal 消息之外单击。 Here is my code:这是我的代码:

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");
});

Thank you!谢谢!

You can check if the alert was dismissed using the cancel button by checking if results .dismiss reason is equal to Swal.DismissReason.cancel :您可以通过检查 results .dismiss reason 是否等于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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM