在使用lwc打开一个tab时需要使用到workspaceAPI,通过调查发现这里给出了两个方案:
经过测试发现第二个方案更简洁一些,并根据打开的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);
});
}