【问题标题】:call other function in toaster onShown event在烤面包机 onShown 事件中调用其他函数
【发布时间】:2018-09-26 11:59:29
【问题描述】:

我有下面的烤面包机代码

    toastr.success("<br /><br /><button type='button' id='confirmationRevertYes' class='btn clear'>Yes</button>",'delete item?',
      {
          closeButton: false,
          allowHtml: true,
          onShown: function (toast) {
              $("#confirmationRevertYes").click(function(){
                hidepanel(); // not working
                this.hidepanel(); // not working
              });
            }
      });

我在外面有一个功能

hidepanel(){
}

当尝试在烤面包机内部调用 onShown 方法时会抛出错误

“HTMLElement”类型上不存在隐藏面板。

这是怎么工作的?

谢谢

【问题讨论】:

  • 我认为发生的情况是您使用了一些第三方组件。如果您使用第三方组件会发生什么,this 将在该组件内部使用。所以要解决这个问题,你可以把这个let me = this放在烤面包机上面,然后me.hidepanel()
  • 谢谢,你的想法成功了
  • 不客气 :) 不确定第三方的事情,但我知道这个解决方案会起作用。

标签: javascript angular typescript angular6 angular-toastr


【解决方案1】:

假设你有一个函数调用hidepanel,使用=&gt;表达式

toastr.success("<br /><br /><button type='button' id='confirmationRevertYes' class='btn clear'>Yes</button>",'delete item?',
  {
      closeButton: false,
      allowHtml: true,
      onShown: (toast) => {
          $("#confirmationRevertYes").click(() =>{ 
            this.hidepanel();  
          });
        }
  });

【讨论】:

  • 编译时没有显示错误,但是点击却抛出错误_this.hidepanel is not a function
  • 你需要定义hidepanel函数
  • 函数存在,如果不存在也会在编译时抛出
  • 还有一个注意事项。它的打字稿文件,所以它的定义就像 hidepanle() {} .. 只有没有函数关键字存在
  • 打字稿文件
猜你喜欢
  • 2022-08-20
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 2022-07-29
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多