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的学习时 ...
随机推荐
- ORM核心功能之导航属性- EFCore和 SqlSugar
导航属性 导航属性是作为ORM核心功能中的核心,在SqlSugar没有支持导航属性前,都说只是一个高级DbHelper, 经过3年的SqlSugar重构已经拥有了一套 非常成熟的导航属性体系,本文不是 ...
- [C#] FFmpeg 音视频开发总结
为什么选择FFmpeg? 延迟低,参数可控,相关函数方便查询,是选择FFmpeg作为编解码器最主要原因,如果是处理实时流,要求低延迟,最好选择是FFmpeg. 如果需要用Opencv或者C#的Emgu ...
- GO 集合 map 使用总结
转载请注明出处: Go语言的集合称为映射(map),它是一种无序的键值对(key-value)的集合,集合是通过键(key)来快速检索值(value)的,键(key)类似于索引,它指向值(value) ...
- 【C#/.NET】探究Task中ConfigureAwait方法
目录 引言 ConfigureAwait方法的作用和原理 ConfigureAwait方法的使用场景 非UI线程场景 避免上下文切换 避免死锁 ConfigureAwait方法的注意事项 在UI ...
- Go优雅的错误处理: 支持错误堆栈, 错误码, 错误链的工具库
地址: https://github.com/morrisxyang/errors 如果觉得有用欢迎 Star 和 PR, 有问题请直接提issue errors 简单的支持错误堆栈, 错误码, 错误 ...
- SRS之StateThreads学习
最近在看SRS的源码.SRS是基于协程开发的,底层使用了StateThreads.所以为了充分的理解SRS源码,需要先学习一下StateThreads.这里对StateThreads的学习做了一些总结 ...
- Codeforces Round #882 (Div. 2) A-D
比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; int a[107]; int ...
- 使用docker构建可动态启动的FreeSWITCH实例
操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 Docker版本:23.0.6 使用docker打包FreeSWITCH可以极大简化部署环节,但针对高可用场景的 ...
- 将mongodb注册成windows(win10)的服务
首先参考这个大佬的文章: 下载mongodb 然后解压 为了方便路径,我改了个名字MongoDB, 然后把它拖到了c盘的Program Files文件夹中 这样他的路径就是 (这个路径在很多地方都要用 ...
- win10使用Docker Desktop启动mysql报错:Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3306 -> 0.0.0.0:0: listen tcp 0.0.0.0:3306:
问题描述 今天上班用wind10电脑启动Docker Desktop使用MySQL,突然间报了一个错,错误如下: Error response from daemon: Ports are not a ...