Salesforce: 通过Lightning Web Component 打开一个 tab

在使用lwc打开一个tab时需要使用到workspaceAPI,通过调查发现这里给出了两个方案:

https://mtr-design.com/news/salesforce-mini-how-to-open-a-subtab-in-console-from-lightning-web-component

经过测试发现第二个方案更简洁一些,并根据打开的tab做了一些调整,代码如下

handleRowAction(event) {
        console.log('on click');
        const action = event.detail.action;
        const row = event.detail.row;
        console.log("row orderNumber:" + row.orderNumber)
        console.log("action:" + action)

        this.orderNumber = row.orderNumber;

        this.invokeWorkspaceAPI('isConsoleNavigation').then(isConsole => {
            if (isConsole) {
                this.invokeWorkspaceAPI('getFocusedTabInfo').then(focusedTab => {
                    this.invokeWorkspaceAPI('openTab', {
                        pageReference: {
                            "type": "standard__component",
                            "attributes": {
                                "componentName": "组件名称"
                            },
                            "state": {
                                "c__orderNumber": row.orderNumber //传递的参数
                            },
                        },
                        focus: true
                    }).then(tabId => {
                        console.log("Solution #2 - SubTab ID: ", tabId);
                        //设置tab显示名称
                        this.invokeWorkspaceAPI('setTabLabel', {
                            tabId: tabId,
                            label: row.orderNumber
                        });
                        // 设置tab图标,不设置会默认显示一个 Loading 图标
                        this.invokeWorkspaceAPI('setTabIcon', {
                            tabId: tabId,
                            icon: "standard:orders"
                            // iconAlt: "SubTab Label Name"
                        });
                    });
                });
            }
        });
    }

    invokeWorkspaceAPI(methodName, methodArgs) {
        return new Promise((resolve, reject) => {
            const apiEvent = new CustomEvent("internalapievent", {
                bubbles: true,
                composed: true,
                cancelable: false,
                detail: {
                    category: "workspaceAPI",
                    methodName: methodName,
                    methodArgs: methodArgs,
                    callback: (err, response) => {
                        if (err) {
                            return reject(err);
                        } else {
                            return resolve(response);
                        }
                    }
                }
            });

            window.dispatchEvent(apiEvent);
        });
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

langzichai

节省了您的时间,来杯奶茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值