前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce LWC学习(四十五) lwc支持Console App控制Tab了

Salesforce LWC学习(四十五) lwc支持Console App控制Tab了

作者头像
Zero-Zhang
发布2023-10-16 18:55:08
1910
发布2023-10-16 18:55:08
举报

本篇参考:https://help.salesforce.com/s/articleView?id=release-notes.rn_lwc_workspaceAPI.htm&release=246&type=5

https://developer.salesforce.com/docs/component-library/bundle/lightning:workspaceAPI/documentation

https://developer.salesforce.com/docs/atlas.en-us.api_console.meta/api_console/sforce_api_console_methods_lightning_workspaceAPI.htm

背景: 针对Console App,我们可以看到官方提供的功能可以修改Tab名称,刷新Tab等功能。我们在针对实际开发时,偶尔也需要有需求操作Tab相关信息,比如修改Tab的名称。以前只能通过Aura Component进行修改,lwc并不支持。

CustomizeTabAura.cmp

代码语言:javascript
复制
<aura:component implements="lightning:isUrlAddressable,flexipage:availableForAllPageTypes"
                access="GLOBAL">
    <lightning:workspaceAPI aura:id="workspace" />
    <aura:attribute name="result" type="String"></aura:attribute>

    <lightning:card>
        <lightning:buttonGroup>
            <lightning:button onclick="{!c.showTabInfo}" label="显示Tab信息"></lightning:button>

            <lightning:button onclick="{!c.changeTabInfo}" label="更改Tab信息"></lightning:button>

            <lightning:button onclick="{!c.addSubTabInfo}" label="打开Sub Tab"></lightning:button>
        </lightning:buttonGroup>
        <div>
            {!v.result}
        </div>
    </lightning:card>
    
</aura:component>

CustomizeTabAuraController.js

代码语言:javascript
复制
({
    showTabInfo: function(component, event, helper) {
        var workspaceAPI = component.find("workspace");

        workspaceAPI.getFocusedTabInfo().then(function(response) {
            
            let information = JSON.stringify(response);
            component.set('v.result', information);
        })
        .catch(function(error) {
            console.log(error);
        });
    },
    changeTabInfo: function(component, event, helper) {
        var workspaceAPI = component.find("workspace");

        workspaceAPI.getFocusedTabInfo().then(function(response) {
            let updatedTitle = 'updated tab';
            workspaceAPI.setTabLabel({
                tabId: response.tabId,
                label: updatedTitle
            })
            workspaceAPI.refreshTab({
                tabId: response.tabId
            })
        })
        .catch(function(error) {
            console.log(error);
        });
    },
    addSubTabInfo: function(component, event, helper) {
        var workspaceAPI = component.find("workspace");

        workspaceAPI.getFocusedTabInfo().then(function(response) {
            workspaceAPI.openSubtab({
                parentTabId: response.tabId, 
                recordId: $A.get("$SObjectType.CurrentUser.Id"),
                focus: true
            })

        })
        .catch(function(error) {
            console.log(error);
        });
    },
})

将组件放在Account详情页效果展示

Aura操作固然很好,但是lightning现在大部分项目是lwc的,性能上会有很好并且整体代码管理也会容易,一个项目如果参杂着太多的aura和lwc本身也不是好事情,官方也逐渐的将aura的功能向lwc进行迁移,比如lwc目前已经支持quick action。同样的在winter 24 release,官方支持通过lwc来操作tab了,尽管目前是beta版本,相信再过两个release就可以GA了。(目前可以在sandbox进行测试)

注:针对此功能,需要开启Lightning Web Security。

 简单的demo如下:

 customizeTabLwc.html

代码语言:javascript
复制
<template>
    <lightning-card>
        <lightning-button-group>
            <lightning-button onclick={showTabInfo} label="显示Tab信息"></lightning-button>

            <lightning-button onclick={changeTabInfo} label="更改Tab信息"></lightning-button>

            <lightning-button onclick={addSubTabInfo} label="打开Sub Tab"></lightning-button>
        </lightning-button-group>
        <div>
            {result}
        </div>
    </lightning-card>
</template>

customizeTabLwc.js: 需要做两个事情

  1. 需要引入lightning/messageService,否则会报错 n.connect is not a function
  2. 引入相关的wire adapter, 将需要的方法引入。

注释部分打开也可以运行,可以通过EnclosingTabId wire adapter获取,也可以通过 getFocusedTabInfo获取tabId

代码语言:javascript
复制
import { LightningElement, track, wire } from 'lwc';
import userId from "@salesforce/user/Id";
import { MessageContext,APPLICATION_SCOPE, publish,subscribe, unsubscribe } from 'lightning/messageService'; 
import { IsConsoleNavigation,EnclosingTabId, getFocusedTabInfo,setTabLabel,refreshTab,openSubtab } from 'lightning/platformWorkspaceApi';
export default class customizeTabLwc extends LightningElement {
    @wire(IsConsoleNavigation) isConsoleNavigation;
    result;
    @wire(EnclosingTabId) tabId;

    showTabInfo(event) {
        if (this.isConsoleNavigation) {
            getFocusedTabInfo().then((tabInfo) => {
                this.result = JSON.stringify(tabInfo);
            }).catch(function(error) {
                console.log(error);
            }); 
        }
    }

    changeTabInfo(event) {
        if (this.isConsoleNavigation) {
            // getFocusedTabInfo().then((tabInfo) => {
            //     setTabLabel(tabInfo.tabId, 'updated tab');
            //     refreshTab(tabInfo.tabId);
            // }).catch(function(error) {
            //     console.log(error);
            // }); 
            setTabLabel(this.tabId, 'updated tab');
        }
    }

    addSubTabInfo(event) {
        if (this.isConsoleNavigation) {
            // getFocusedTabInfo().then((tabInfo) => {
            //     openSubtab(tabInfo.tabId, { recordId: userId, focus: true });
            // }).catch(function(error) {
            //     console.log(error);
            // }); 
            openSubtab(this.tabId, { recordId: userId, focus: true });
        }
    }
}

运行效果和上述相同。

总结:篇中介绍基于lwc控制tab的方法,官方提供了很多方法,感兴趣的小伙伴可以自行查看。篇中有错误地方欢迎指出,有不懂欢迎留言。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-09-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档