【问题标题】:Error while executing showtoast in salesforce lightning在 Salesforce Lightning 中执行 showtoast 时出错
【发布时间】:2019-02-04 16:35:39
【问题描述】:

以下代码在执行 toastEvent.setParams 语句时抛出错误。不确定我错过了什么,或者它在 spring'19 中被弃用了吗?

loadContacts : function(cmp) {
    var action = cmp.get("c.getContacts");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === 'SUCCESS') {
            cmp.set('v.contacts', response.getReturnValue());
            cmp.set('v.contactList', response.getReturnValue());
            this.updateTotal(cmp);
        }
        console.log('Here');
        var toastEvent = $A.get("e.force:showToast");
        if (state === 'SUCCESS') {
            toastEvent.setParams({
                "title" : 'Success!',
                "message" : 'Your contacts have been loaded successfully.'
            });
        }
        else {
            toastEvent.setParams({
                "title" : "Error!",
                "message" : "Something has gone wrong."
            });
        }
        toastEvent.fire();
    });
    $A.enqueueAction(action);
},

这是错误的屏幕截图:

【问题讨论】:

    标签: salesforce salesforce-lightning


    【解决方案1】:

    From the docs:

    此事件由 one.app 容器处理。它在 Lightning Experience、Salesforce 应用程序和 Lightning 社区中受支持。

    当您尝试在 one.app 容器上下文之外获取 $A.get("e.force:showToast") 时会弹出该错误(例如,如果您将组件放在 Lightning 应用程序中进行测试,而不是将其拖到构建器中的记录页面上)。 $A.get("e.force:showToast") 返回未定义,在未定义时调用 setParams 会引发错误。

    尝试将您的组件拖到记录详细信息页面或社区上,或从 Lighting 组件创建一个选项卡。如果需要在 one.app 上下文之外使用组件,则需要自己实现显示/隐藏 toast 逻辑。

    【讨论】:

      【解决方案2】:

      您可以使用lightning:notificationsLibrary。在您的 aura 组件中,添加:

      <lightning:notificationsLibrary aura:id="notifLib"/>
      

      并将您的辅助方法更改为:

      loadContacts : function(cmp) {
          var action = cmp.get("c.getContacts");
          action.setCallback(this, function(response){
              var state = response.getState();
              if (state === 'SUCCESS') {
                  cmp.set('v.contacts', response.getReturnValue());
                  cmp.set('v.contactList', response.getReturnValue());
                  this.updateTotal(cmp);
              }
              console.log('Here');
              var toastEvent = $A.get("e.force:showToast");
              if (state === 'SUCCESS') {
                  cmp.find('notifLib').showToast({
                      "title" : 'Success!',
                      "message" : 'Your contacts have been loaded successfully.'
                      "variant": 'success',
                      "mode": 'sticky'
                  });
              }
              else {
                  cmp.find('notifLib').showToast({
                      "title" : "Error!",
                      "message" : "Something has gone wrong."
                      "variant": 'error',
                      "mode": 'sticky'
                  });
              }
          });
          $A.enqueueAction(action);
      },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-10
        • 2018-10-24
        • 2018-12-11
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多