Reputation: 11
How could we allow User to use only one LWC instance in salesforce?
I'm a newbie in salesforce lightning web components so wanted to set a permission or a code could be useful.
The expected results should be: only one LWC instance should appear, should be visible or user should be allowed to only drag and drop once that would suffice the requirement.
Upvotes: 1
Views: 874
Reputation: 111
As David said, it is not recommended to limit the instances of a component on a page. However, there is a way to do it. In your LWC (say, 'LWC1'), import a method from the controller of another LWC (say 'LWC2'). In this method, maintain a counter to keep track of the number of times the method was invoked and return the value. In LWC1, invoke this method and decide whether to render the UI based on the returned value. You can, thus, control how many instances of the component you want rendered on the page at one time. However, note that you won't be able to control which instance is rendered i.e the any of the instances of the component could be rendered at random. This principle of a shared back-end controller is what the pubsub recipe is based on
Upvotes: 1
Reputation: 2759
You seem to be asking about permissioning your component while using the Lightning App Builder to create Lightning pages as a Salesforce administrator.
Your component can control the contexts within which it is available by modifying your Component Configuration File, where your targets
element defines the types of page for which it can be used. However, you cannot limit the count of components that can be placed on a single page, nor should you - it's often necessary, for example, to place multiple components within different containers that are conditionally rendered based upon the viewing user or record attributes.
Upvotes: 0