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;
        }
    }
相关推荐
卡兰芙的微笑7 分钟前
get_property --Cmakelist之中
前端·数据库·编辑器
覆水难收呀10 分钟前
三、(JS)JS中常见的表单事件
开发语言·前端·javascript
猿来如此呀17 分钟前
运行npm install 时,卡在sill idealTree buildDeps没有反应
前端·npm·node.js
hw_happy23 分钟前
解决 npm ERR! node-sass 和 gyp ERR! node-gyp 报错问题
前端·npm·sass
FHKHH27 分钟前
计算机网络第二章:作业 1: Web 服务器
服务器·前端·计算机网络
视觉小鸟1 小时前
【JVM安装MinIO】
前端·jvm·chrome
二川bro1 小时前
【已解决】Uncaught RangeError: Maximum depth reached
前端
qq22951165023 小时前
python毕业设计基于django+vue医院社区医疗挂号预约综合管理系统7918h-pycharm-flask
前端·vue.js·express
八了个戒3 小时前
Koa (下一代web框架) 【Node.js进阶】
前端·node.js
Sca_杰3 小时前
vue2使用npm引入依赖(例如axios),报错Module parse failed: Unexpected token解决方案
前端·javascript·vue