Hi boys…
I need your help…
I’m newbie with web components, and i have created with success a polymer app with vaadin web components.
But, if i want to bind an event, like a click in a button, nothing is fired…
For example, this is my view2.js page
import { html } from 'lit-element';
import { PageViewElement } from './page-view-element.js';
import '@vaadin/vaadin-button/vaadin-button.js';
import '@vaadin/vaadin-combo-box/vaadin-combo-box.js';
import { SharedStyles } from './shared-styles.js';
class MyView2 extends (PageViewElement) {
static get properties() {
return {
// This is the data from the store.
_clicks: { type: Number },
_value: { type: Number }
};
}
static get styles() {
return [
SharedStyles
];
}
render() {
return html`<vaadin-button>Button</vaadin-button>
clicked <span>0</span> times.
<script>
document.querySelector('vaadin-button').addEventListener('click', () => alert('Hello World!'));
</script>`;
}
}
window.customElements.define('my-view2', MyView2);
The html returned by the render function is rendered inside your component’s shadow DOM. This content is isolated from the document scope, so document.querySelector doesn’t find the elements inside the component.
To query the internals of the shadow DOM, you can use this.shadowRoot.querySelector.
Thanks for your reply
I understand how everything works with Lit-element but i want to work with Vaadin elements…
Do you have some example of Polymer + Vaadin listeners? (or, if you can, you can modify my example code)
Thanks.
This works perfectly as long as the <vaadin-button> is in the light DOM. This means that it is not located inside a shadow DOM of another web component. If the <vaadin-button> was attached directly to a traditional html document, it would work. In your case, it is in the shadow DOM of <my-view2> component.
Since the button is in the shadow DOM, you need to query it from the parent component’s shadow root, like this (inside your class):