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;
        }
    }
相关推荐
容沁风28 分钟前
react路由Cannot GET错误
前端·react.js·前端框架·sharp7
少云清32 分钟前
【UI自动化测试】6_web自动化测试 _浏览器操作
前端·web自动化测试
globaldomain37 分钟前
立海世纪:.com和.net域名哪个更适合你的网站
大数据·前端·人工智能·新媒体运营·国外域名·域名注册
phltxy1 小时前
Vue Router:从入门到实战
前端·javascript·vue.js
Zhencode2 小时前
Vue3核心运行时之runtime-core
前端·javascript·vue.js
木斯佳2 小时前
前端八股文面经大全:腾讯WXG技术架构前端面试(2025-11-19)·面经深度解析
前端·面试·架构
感性的程序员小王2 小时前
HTTPS页面请求HTTP接口失败?一文讲透Mixed Content
前端·后端
用户600071819102 小时前
【翻译】我竟渐渐迷上了生成器的设计巧思
前端
Wect2 小时前
LeetCode 104. 二叉树的最大深度:解题思路+代码解析
前端·算法·typescript
千寻girling2 小时前
面试官 : “ 请说一下 JS 的常见的数组 和 字符串方法有哪些 ? ”
前端·javascript·面试