https://developer.salesforce.com/docs/component-library/bundle/lightning:workspaceAPI/documentation
背景: 針對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: 需要做兩個事情
- 需要引入lightning/messageService,否則會報錯 n.connect is not a function
- 引入相關的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的方法,官方提供了很多方法,感興趣的小夥伴可以自行查看。篇中有錯誤地方歡迎指出,有不懂歡迎留言。