如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件:

2.调用utility bar api的方式有两种:

方法一,通过lwc调用:

import {LightningElement,api ,wire   } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import CallCenterMessageChannel from '@salesforce/messageChannel/callCenterChannelName__c';
import{open , getAllUtilityInfo} from 'lightning/platformUtilityBarApi';
import { CloseActionScreenEvent } from 'lightning/actions';

export default class CallCenterMessageService extends LightningElement {

    @api selectedIds; // 从 Flow 传递的记录 ID
    @api recordId
    @wire(MessageContext)
    messageContext;
    // 当组件加载时处理记录 ID
    async connectedCallback() {
        // console.log('Selected Record IDs: ', this.selectedIds);
        console.log('record Id: ', this.recordId);
        debugger
        await this.handleOpen();
        setTimeout(() => {
            this.handlePublish();
            this.closeQuickAction()
        }, 1000); // 延迟1000毫秒,可以根据情况调整时间

    }
    //调用utility api打开utility bar
    async handleOpen(){
        debugger
        const unitilyInfo=await getAllUtilityInfo();
        for (let i = 0; i < unitilyInfo.length; i++) {
            console.log('util at index', i, ':', unitilyInfo[i]);
            if(unitilyInfo[i].utilityLabel==='呼叫控制台'){
                open(unitilyInfo[i].id);
            }
        }
    }
    //publish事件至channel
    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());
    }


}

方式二,通过aura调用:

handleGetUtilityInfo: function (component, event, helper) {
        debugger
        var utilityBarAPI = component.find("utilitybar");
        console.log('utilityBarAPI--->' + JSON.stringify(utilityBarAPI));
        console.log('utilityBarAPI--->' + JSON.stringify(utilityBarAPI.getAllUtilityInfo()));
        utilityBarAPI.getAllUtilityInfo().then(function (response) {
            console.log(response.length);
            console.log(JSON.stringify(response));
            for (let i = 0; i < response.length; i++) {
                var myUtilityInfo = response[i];
                console.log("myUtilityInfo--》" + JSON.stringify(myUtilityInfo));
                if (myUtilityInfo.utilityLabel === '呼叫控制台') {
                    utilityBarAPI.openUtility({
                        utilityId: myUtilityInfo.id
                    });
                }
            }
        })
        setTimeout(() => {
            // this.handlePublish();
            
        }, 1000); // 延迟1000毫秒,可以根据情况调整时间
    },

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

3.传递参数,通过message channel publish事件,在utility bar中lwc/aura组件订阅事件接受参数:

  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"));
            
        }
    },

tips:publish事件之前,需要提前open utility bar,否则utility bar中组件无法订阅

相关推荐
燃先生._.4 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
高山我梦口香糖5 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
black^sugar6 小时前
纯前端实现更新检测
开发语言·前端·javascript
2401_857600957 小时前
SSM 与 Vue 共筑电脑测评系统:精准洞察电脑世界
前端·javascript·vue.js
2401_857600957 小时前
数字时代的医疗挂号变革:SSM+Vue 系统设计与实现之道
前端·javascript·vue.js
GDAL7 小时前
vue入门教程:组件透传 Attributes
前端·javascript·vue.js
小白学大数据7 小时前
如何使用Selenium处理JavaScript动态加载的内容?
大数据·javascript·爬虫·selenium·测试工具
2402_857583497 小时前
基于 SSM 框架的 Vue 电脑测评系统:照亮电脑品质之路
前端·javascript·vue.js
java_heartLake8 小时前
Vue3之性能优化
javascript·vue.js·性能优化