Salesforce Message Service 跨组件之间通信

1.创建MessageChannels,在default目录下新建文件夹messageChannels:

2.新建channel文件,命名规则:name+".messageChannel-meta.xml":

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    <description>This is a callcenter Lightning Message Channel.</description>
    <isExposed>true</isExposed>
    <lightningMessageFields>
        <description>This is the record Id that changed</description>
        <fieldName>recordId</fieldName>
    </lightningMessageFields>
    <masterLabel>callCenterMessageChannel</masterLabel>
</LightningMessageChannel>

lightningMessageFields:配置传递参数

3.在LWC中publish Channel,import的channel是messagechannel中的name+"__c":

javascript 复制代码
import {LightningElement,api ,wire } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import CallCenterMessageChannel from '@salesforce/messageChannel/callCenterChannelName__c';

export default class CallCenterMessageService extends LightningElement {

    @api recordId
    @wire(MessageContext)
    messageContext;
    // 当组件加载时处理记录 ID
    async connectedCallback() {
        // console.log('Selected Record IDs: ', this.selectedIds);
        console.log('record Id: ', this.recordId);
        debugger
       
        this.handlePublish();
          

    }

    handlePublish(){
        // debugger
        // console.log('handlePublish--Selected Record IDs: ', JSON.stringify(this.selectedIds));
        const payload = { recordId:  this.recordId};
        publish(this.messageContext, CallCenterMessageChannel, payload);
    }

    closeQuickAction() {
        this.dispatchEvent(new CloseActionScreenEvent());
    }


}

4.在aura中publish & 订阅事件:

publish代码:

javascript 复制代码
cmp:
 <lightning:messageChannel type="callCenterChannelName__c"
                              aura:id="callCenterMessageChannel"/>

js:
handlePublish: function (cmp, event, helper) {
        var payload = {
            recordId: cmp.get("v.recordId")
        };
        cmp.find("callCenterMessageChannel").publish(payload);
    }

订阅事件:

javascript 复制代码
cmp:
<lightning:messageChannel type="callCenterChannelName__c" onMessage="{!c.handleCallCenterChannel}" scope="APPLICATION"/>

js:
  handleCallCenterChannel: function (cmp, message, helper) {
        // debugger;
        console.log("订阅信息!!");
        if (message != null && message.getParam("recordId") != null) {
            console.log("recordId--->" + message.getParam("recordId"));
            cmp.set("v.recordId", message.getParam("recordId"));
            
        }
    }

5.visualforce page 中publish & 订阅:

javascript 复制代码
 function publishMC() {
        const message = {
        record: "a0WIm000000cDYrMAM"
        };
        sforce.one.publish(callCenter, message);
        }

        //订阅事件
        function subscribeMC() {
        if (!subscriptionToMC) {
        subscriptionToMC = sforce.one.subscribe(SAMPLEMC, displayMessage);
        }
        }
        //取消订阅事件
        function unsubscribeMC() {
        if (subscriptionToMC) {
        sforce.one.unsubscribe(subscriptionToMC);
        subscriptionToMC = null;
        }
    }
相关推荐
juruiyuan1111 小时前
FFmpeg3.4 libavcodec协议框架增加新的decode协议
前端
Peter 谭1 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript
LuckyLay3 小时前
React百日学习计划——Deepseek版
前端·学习·react.js
gxn_mmf3 小时前
典籍知识问答重新生成和消息修改Bug修改
前端·bug
hj10433 小时前
【fastadmin开发实战】在前端页面中使用bootstraptable以及表格中实现文件上传
前端
乌夷3 小时前
axios结合AbortController取消文件上传
开发语言·前端·javascript
晓晓莺歌3 小时前
图片的require问题
前端
码农黛兮_464 小时前
CSS3 基础知识、原理及与CSS的区别
前端·css·css3
水银嘻嘻4 小时前
web 自动化之 Unittest 四大组件
运维·前端·自动化
(((φ(◎ロ◎;)φ)))牵丝戏安4 小时前
根据输入的数据渲染柱形图
前端·css·css3·js