如何在页面调用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中组件无法订阅

相关推荐
Smile_Gently2 小时前
前端:最简单封装nmp插件(组件)过程。
前端·javascript·vue.js·elementui·vue
nihui1237 小时前
Uniapp 实现顶部标签页切换功能?
javascript·vue.js·uni-app
一 乐9 小时前
高校体育场管理系统系统|体育场管理系统小程序设计与实现(源码+数据库+文档)
前端·javascript·数据库·spring boot·高校体育馆系统
shengmeshi9 小时前
vue3项目img标签动态设置src,提示:ReferenceError: require is not defined
javascript·vue.js·ecmascript
BillKu9 小时前
vue3中<el-table-column>状态的显示
javascript·vue.js·elementui
祈澈菇凉9 小时前
ES6模块的异步加载是如何实现的?
前端·javascript·es6
我爱学习_zwj9 小时前
4.从零开始学会Vue--{{组件通信}}
前端·javascript·vue.js·笔记·前端框架
*TQK*10 小时前
✨1.HTML、CSS 和 JavaScript 是什么?
前端·javascript·css·html
优联前端10 小时前
DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方DeepSeek接入)
javascript·pycharm·ai编程·前端开发·优联前端·deepseek
萧大侠jdeps10 小时前
el-select 添加icon
前端·javascript·vue.js