如何显示带有动态变量的 toastr 消息

How to Show a toastr message with dynamic variables

我正在使用 Toastr 在 UI 中显示消息弹出窗口。

我正在通过 Ajax 向服务器发送请求,作为响应,我正在发送以下响应

echo json_encode(
                    array(
                            "type" => "error",
                            "message" => $error,
                            "status" => "Error While Updating!"
                         )
                );

我正在使用 resp.type 来显示动态 toastr,下面是我的 toastr 代码

.done(function(resp)
    {
        toastr.resp.type(resp.message, resp.status,{progressBar:!0,showMethod:"slideDown",hideMethod:"slideUp",timeOut:2e3,preventDuplicates: true,positionClass: "toast-bottom-right"});
    });

上面代码的问题在于,当代码为 运行 时,它会抛出 Uncaught TypeError: toastr.type is not a function

的错误消息

谁能帮我找出问题所在或正确的解决方案

您不能嵌入 toastr.resp.type,它无效,因此会引发错误。

根据我对问题的理解,下面的代码可以按照您的意愿运行

.done(function(resp)
   {
       toastr[resp.type](resp.message, resp.status,{progressBar:!0,showMethod:"slideDown",hideMethod:"slideUp",timeOut:2e3,preventDuplicates: true,positionClass: "toast-bottom-right"});
   });

请查看此作为参考:https://github.com/CodeSeven/toastr/issues/203

function showToast(message, timeout, type) {
      type = (typeof type === 'undefined') ? 'info' : type;
      toastr.options.timeOut = timeout;
      toastr[type](message);
  }

showToast('Hello Toastr!", 15000, 'warning');