How can I access to named slot as a variable !? #3405
Answered
by
justinfagnani
mojtaba-afraz
asked this question in
Q&A
-
Hello |
Beta Was this translation helpful? Give feedback.
Answered by
justinfagnani
Oct 30, 2022
Replies: 1 comment
-
You can use import {LitElement, html} from 'lit';
import {customElement, query} from 'lit/decorators.js';
@customElement('my-element')
class MyElement extends LitElement {
@query('slot[name=foo]')
_fooSlot!: HTMLSlotElement;
render() {
return html`
<button @click=${this._go}>Go</button>
<slot name="foo"></slot>
`;
}
_go() {
// If you're not using decorators, you can querySelector:
// const fooSlot = this.shadowRoot.querySelector('slot[name=foo]');
const fooSlottedElement = this._fooSlot.assignedElements(true);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mojtaba-afraz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
this.shadowRoot.querySelector()
or the@query()
decorator: