Skip to content

Autoloader question #2324

Answered by claviska
ourson66 asked this question in Help
Discussion options

You must be logged in to vote

A quick way to guarantee an element exists before accessing properties or methods is:

customElements.whenDefined('sl-alert').then(() => {
  // The component has been fully registered
});

With top-level await, you can also do something like this early on in your app:

await customElements.whenDefined('sl-alert');

Or, for multiple components:

await Promise.all([
  customElements.whenDefined('sl-alert'),
  customElements.whenDefined('sl-another-tag'),
  // ...
]);

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ourson66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants