Salesforce LWC学习(四十五) lwc支持Console App控制Tab了
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的方法,官方提供了很多方法,感兴趣的小伙伴可以自行查看。篇中有错误地方欢迎指出,有不懂欢迎留言。
Salesforce LWC学习(四十五) lwc支持Console App控制Tab了的更多相关文章
- Salesforce LWC学习(四十) dynamic interaction 浅入浅出
本篇参考: Configure a Component for Dynamic Interactions in the Lightning App Builder - Salesforce Light ...
- Salesforce LWC学习(四十二) getRecordNotifyChange已弃用
本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get ...
- Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案
本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...
- Salesforce LWC学习(二十五) Jest Test
本篇参看: https://trailhead.salesforce.com/content/learn/modules/test-lightning-web-components https://j ...
- Salesforce LWC学习(三十五) 使用 REST API实现不写Apex的批量创建/更新数据
本篇参考: https://developer.salesforce.com/docs/atlas.en-us.224.0.api_rest.meta/api_rest/resources_compo ...
- Salesforce LWC学习(四十) datatable的dynamic action的小坑浅谈
本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentatio ...
- Salesforce LWC学习(三十八) lwc下如何更新超过1万的数据
背景: 今天项目组小伙伴问了一个问题,如果更新数据超过1万条的情况下,有什么好的方式来实现呢?我们都知道一个transaction只能做10000条DML数据操作,那客户的操作的数据就是超过10000 ...
- shell 学习四十五天---xargs
当 find 产生一个文件列表时,该列表提供给另一个命令有时是很有用的.案例: $touch abc.c erd.c oiy.c $ll ./erd.c ./abc.c ./oiy.c $find - ...
- Java开发学习(四十五)----MyBatisPlus查询语句之映射匹配兼容性
1.映射匹配兼容性 我们已经能从表中查询出数据,并将数据封装到模型类中,这整个过程涉及到一张表和一个模型类: 之所以数据能够成功的从表中获取并封装到模型对象中,原因是表的字段列名和模型类的属性名一样. ...
- Salesforce LWC学习(三十) lwc superbadge项目实现
本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...
随机推荐
- 【RocketMQ】NameServer总结
NameServer是一个注册中心,提供服务注册和服务发现的功能.NameServer可以集群部署,集群中每个节点都是对等的关系(没有像ZooKeeper那样在集群中选举出一个Master节点),节点 ...
- # 代码随想录算法训练营Day4|24.两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题02.07.链表相交 142.环形链表Ⅱ
24.两两交换链表中的节点 题目链接:24.两两交换链表中的节点 总体思路: 两两交换链表中的节点使用虚拟头节点可以更方便地进行交换,这样头节点和普通节点可以以同一种方式进行. 虚拟头结点的建设代码: ...
- Python日期带时区转换工具类总结
目录 1.背景 2. 遇到的坑 3. 一些小案例 3.1 当前日期.日期时间.UTC日期时间 3.2 昨天.昨天UTC日期.昨天现在这个时间点的时间戳 3.3 日期转时间戳 3.4 时间戳转日期 3. ...
- 新风向标:学术界开始从 Python 转向 Rust
作者 | Jeffrey M. Perkel 策划 | Tina 来源 | Rust语言中文社区 Rust 现在已经越来越受到科学家们的欢迎了,比起 Python,Rust 有着更高效的性能,同时在社 ...
- Tr0ll-1项目实战
前言 Tr0ll的灵感来源于OSCP实验室内机器的不断拖动. 目标很简单,获取root并从/root目录中获取Proof.txt. 不适合那些容易受挫的人!公平的警告,前方有巨魔! 靶机环境 kali ...
- 4. SpringMVC获取请求参数
1. 通过 ServletAPI 获取 将 HttpServletRequest 作为控制器方法的形参 , 此时 HttpServletRequest 类型的参数表示封装了当前请求的请求报文的对象 ...
- java调用WebService(未完成)记录篇
背景: 因工作需要和一个Sap相关系统以WebService的方式进行接口联调,之前仅听过这种技术,但并没有实操过,所以将本次开发相关的踩坑进行记录 通过一个实例来认识webservice 服务端 首 ...
- React后台管理系统11 配置项目初始化展开代码
在上一文中,我们已经配置好了,刷新默认打开选中的样式,但是如果是在/page3/1,这种的,并没有选中到/page3里面的/page3/1,这个地方来,所以我们需要解决的就是这几个问题: 思路如下: ...
- 使用Kettle定时从数据库A刷新数据到数据库B
一.需求背景 由于项目场景原因,需要将A库(MySQL)中的表a.表b.表c中的数据定时T+1 增量的同步到B库(MySQL).这里说明一下,不是数据库的主从备份,就是普通的数据同步.经过技术调研,发 ...
- ERP查询Q报表开发代码
一,按照一般ERP开发流程可参考ERP开发流程,直到下载程序. 当我们的查询页签存在栏位需要判断或者特殊处理时,在global中的自定义模组变数下添加,例如: 1 #add-point:自定義模組變數 ...