本篇参考: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的方法,官方提供了很多方法,感兴趣的小伙伴可以自行查看。篇中有错误地方欢迎指出,有不懂欢迎留言。

Salesforce LWC学习(四十五) lwc支持Console App控制Tab了的更多相关文章

  1. Salesforce LWC学习(四十) dynamic interaction 浅入浅出

    本篇参考: Configure a Component for Dynamic Interactions in the Lightning App Builder - Salesforce Light ...

  2. Salesforce LWC学习(四十二) getRecordNotifyChange已弃用

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get ...

  3. Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...

  4. Salesforce LWC学习(二十五) Jest Test

    本篇参看: https://trailhead.salesforce.com/content/learn/modules/test-lightning-web-components https://j ...

  5. Salesforce LWC学习(三十五) 使用 REST API实现不写Apex的批量创建/更新数据

    本篇参考: https://developer.salesforce.com/docs/atlas.en-us.224.0.api_rest.meta/api_rest/resources_compo ...

  6. Salesforce LWC学习(四十) datatable的dynamic action的小坑浅谈

    本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentatio ...

  7. Salesforce LWC学习(三十八) lwc下如何更新超过1万的数据

    背景: 今天项目组小伙伴问了一个问题,如果更新数据超过1万条的情况下,有什么好的方式来实现呢?我们都知道一个transaction只能做10000条DML数据操作,那客户的操作的数据就是超过10000 ...

  8. shell 学习四十五天---xargs

    当 find 产生一个文件列表时,该列表提供给另一个命令有时是很有用的.案例: $touch abc.c erd.c oiy.c $ll ./erd.c ./abc.c ./oiy.c $find - ...

  9. Java开发学习(四十五)----MyBatisPlus查询语句之映射匹配兼容性

    1.映射匹配兼容性 我们已经能从表中查询出数据,并将数据封装到模型类中,这整个过程涉及到一张表和一个模型类: 之所以数据能够成功的从表中获取并封装到模型对象中,原因是表的字段列名和模型类的属性名一样. ...

  10. Salesforce LWC学习(三十) lwc superbadge项目实现

    本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...

随机推荐

  1. Taurus.mvc .Net Core 微服务开源框架发布V3.1.7:让分布式应用更高效。

    前言: 自首个带微服务版本的框架发布:Taurus.MVC V3.0.3 微服务开源框架发布:让.NET 架构在大并发的演进过程更简单 已经过去快1年了,在这近一年的时间里,版本经历了N个版本的迭代. ...

  2. Python 安装教程,新手入门(超详细)含Pycharm开发环境安装教程

    目录 一.Python介绍 二.Python安装教程 (一)Python的下载 (二)Python的安装 三.Pycharm开发工具的安装 (一)Pycharm介绍 (二)Pycharm的下载 (三) ...

  3. java利用jni调用dll方法

    准备工作: 需要用到的插件jni4net:这个需要去官网下载:https://sourceforge.net/projects/jni4net/files/ (1)     jni4net 是一个开源 ...

  4. windows服务启动时提示找不到指定路径的问题

    我是自己写了一个windows服务,并且在之前一直运行良好,上周四晚上之后,竟然莫名其妙的停止了,我登上远程服务器,才发现,该服务已经停止,当我手动打开该服务时,提示我如下错误,找不到指定路径:. 一 ...

  5. 云上使用 Stable Diffusion ,模型数据如何共享和存储

    随着人工智能技术的爆发,内容生成式人工智能(AIGC)成为了当下热门领域.除了 ChatGPT 之外,文本生成图像技术更令人惊艳. Stable Diffusion,是一款开源的深度学习模型.与 Mi ...

  6. 10分钟讲清int 和 Integer 的区别

    其实在Java编程中,int和Integer都是非常常用的数据类型,但它们之间存在一些关键的区别,特别是在面向对象编程中.所以接下来,就让我们一起来探讨下关于int和Integer的区别这个问题吧. ...

  7. 云享·案例丨打造数智物流底座,华为云DTSE助力物联云仓解锁物流新“速度”

    摘要:华为云凭借领先的技术和快速响应的开发者支持服务,助力物联亿达实现云上资源高可用.提升系统安全性与稳定性,为物联亿达提供了扎实的数字化基础. 本文分享自华为云社区<云享·案例丨打造数智物流底 ...

  8. 【Redis】八股文(一)

    什么是Redis 基于key-value存储结构的NoSQL数据库 提供了String, Map, Set, ZSet, List等多种数据类型 功能丰富:支持发布订阅模式,能够为数据设置过期时间,能 ...

  9. 开源BaaS平台Supabase介绍

    Supabase 介绍 Supabase 是一个开源的 Firebase 替代品,以BaaS的形式向各种应用程序提供了一系列的后端功能,可以帮助开发者更快地构建产品. 对于想快速实现一个产品而言,如果 ...

  10. 「学习笔记」FHQ-treap

    FHQ-treap,即无旋 treap,又称分裂合并 treap,支持维护序列,可持久化等特性. FHQ-treap 有两个核心操作,分裂 与 合并.通过这两个操作,在很多情况下可以比旋转 treap ...