问题
自定义了一个Aura Component,如何限制只对Account Lightning Page可见?
探索
了解到Aura Component中有2个地方可以限制组建的可见性:
#1. 通过在.cmp文件中指定implements
粗粒度限制;
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global">
#2. 通过配置.js-meta.xml
文件细粒度限制;
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>AccountWrapper</masterLabel>
<description>This component is only applicable for Account record.</description>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
验证
通过Lightning Builder在Contact记录上依然可以看到该组件,显然探索步骤中的#1,2不成功。
因此通过ChatGPT得到如下答案:
结论
并不能通过探索#1,2中的步骤达到目的,需要在js中check sobjectType是否符合要求。
下图就是项目中通过Js check限制自定义组件AccountPeopleWrapper
仅能被Account, Contact, Lead使用的最终使用的方案: