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;
        }
    }
相关推荐
夏梦春蝉3 分钟前
ES6从入门到精通:其他特性
前端·javascript·es6
2301_1472583699 分钟前
7月1日作业
java·前端·算法
汪子熙10 分钟前
Angular 应用中手动调用 subscribe 方法的时机与实践探讨
前端
MiyueFE42 分钟前
14 个逻辑驱动的 UI 设计技巧,助您改善任何界面
前端·设计
啃火龙果的兔子1 小时前
前端单元测试覆盖率工具有哪些,分别有什么优缺点
前端·单元测试
「、皓子~1 小时前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了1 小时前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_1 小时前
Ajax 入门
前端·javascript·ajax
京东零售技术2 小时前
京东小程序JS API仓颉改造实践
前端
老A技术联盟2 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序