简体   繁体   English

无法关闭动态闪电组件

[英]Not being able to close the dynamic lightning component

I am trying to create a dynamic lightning component with the help of $A.createComponents.我试图在 $A.createComponents 的帮助下创建一个动态闪电组件。

 $A.createComponents([
         ["c:SubmitForApprovalBody",{oppId:recordId}],
         [ "c:SubmitForApprovalFooter", { okLabel : "Confirm"}]            

       ],
       function(components, status){
           console.log('status : '+status);
           if (status === "SUCCESS") {
               modalBody = components[0];
               modalFooter = components[1];                 
               component.find('modalLib').showCustomModal({
                   header: "Submit for Approval",
                   body: modalBody,
                   footer:modalFooter,
                   showCloseButton: false,
                   closeCallback: function() {
                      alert('you decided to close');
                   }
               })
           }
       }
    );

The above is fine.以上没问题。 And, I want to close the component when the user clicks on the ok button in SubmitForApprovalFooter.而且,我想在用户单击 SubmitForApprovalFooter 中的确定按钮时关闭该组件。

I have used the below one in SubmitForApprovalFooter.我在 SubmitForApprovalFooter 中使用了以下一个。

 ({
     handleOK : function(cmp, event, helper) {
        $A.get("e.force:closeQuickAction").fire();
     }
  })

But nothing happens and the component does not disappear.但是什么也没有发生,组件也不会消失。 Any help is much appreciated.任何帮助深表感谢。

So I've faced the same problem you have a few times.所以我遇到过你几次相同的问题。 The trick is to save the modal promise as an aura:attribute on your component.诀窍是将模态承诺保存为aura:attribute组件上。

  1. In your component c:SubmitForApprovalFooter , create a parameter on the component called onClickAction of type Aura.Action在您的组件c:SubmitForApprovalFooter ,在名为onClickAction的组件上创建一个onClickAction类型的Aura.Action
  2. Set that attribute in your js as component.get("c.handleModalClose")在你的 js 中将该属性设置为component.get("c.handleModalClose")
  3. In the handleModalClose function, get the modal promise parameter and close the modal from the promise.handleModalClose函数中,获取modal promise 参数并从promise 中关闭modal。 (see below) (见下文)
({
    yourAction : function(component, event, helper) {
        $A.createComponents([
            ["c:SubmitForApprovalBody",{oppId:recordId}],
            //Notice the `onclickAction` being set
            //If you experience issues with this then use component.getReference("c.handleModalClose")
            [ "c:SubmitForApprovalFooter", { okLabel : "Confirm",
                                            "onclickAction":component.get("c.handleModalClose")
                                        }]
          ],
          function(components, status){
              console.log('status : '+status);
              if (status === "SUCCESS") {
                  modalBody = components[0];
                  modalFooter = components[1];
                  //Set a variable containing the promise                 
                    var modalPromise = component.find('modalLib').showCustomModal({
                      header: "Submit for Approval",
                      body: modalBody,
                      footer:modalFooter,
                      showCloseButton: false,
                      closeCallback: function() {
                         alert('you decided to close');
                      }
                  })
                  //Set the modalPromise as an attribute on your component, type is `Object`
                  //So, `<aura:attribute name="modalPromise" type="Object"/>`
                  component.set("v.modalPromise",modalPromise);
              }
          }
       );
    },
    handleModalClose : function(component,event,helper){
        //I use this all the time now, otherwise aura may try to 
        //grab a modal promise that has been destroyed already
        event.stopPropagation();
        var modPromise = component.get("v.modalPromise");
        modPromise.then(function(m){
            m.close();

        });
    }
})

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

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