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

本篇參考: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

<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

({
    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

<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

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的方法,官方提供了很多方法,感興趣的小夥伴可以自行查看。篇中有錯誤地方歡迎指出,有不懂歡迎留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章