【问题标题】:Sweetalert Promise issueSweetalert 承诺问题
【发布时间】:2018-01-09 18:41:48
【问题描述】:

正在寻找但找不到任何答案。

我遇到了 Promise() 问题。

一直在使用 Sweetalert,所以创建了一个脚本:

<button onclick="showAlert();">some button</button>
<script> 
/**
 * @constuctor
*/
function Obj() { };

/**
 * @method getInst
 * @param arguments
 */
Obj.prototype.getInst = function () {
  swal.apply(this, arguments);
};

/**
 * @instance
 */
var newInst = new Obj();

function showAlert() {
  newInst.getInst({
    title: "Good job!",
    text: "You clicked the button!",
    icon: "success"
  });
};

并将参数应用于 swal 对象工作正常。然而,应用与承诺(Sweetalert 以这种方式工作)对抗的回调不能被链接并应用于对象并且不起作用:

function showAlert() {
  newInst.getInst("Click on either the button or outside the modal.")
  .then((value) => {
  swal(`The returned value is: ${value}`);
});
};

感谢您的回复。

【问题讨论】:

  • 为了让它工作,你的getInst必须返回一个新的Promise
  • 您的getInst 方法缺少return 语句。
  • 是的,也注意到了这一点——尽管添加这个并没有解决问题。

标签: javascript sweetalert2


【解决方案1】:

好的,已经完成了。

如果有人感兴趣,您只需将 Sweetalert 对象直接传递给构造函数:

Obj.prototype.getInst = function () {
  swal(opts);
};

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多