I've successfully added an eventlistener for my Lit element component inside my Typescript code:
async firstUpdated() {
await this.updateComplete;
// Add an event listener to our event-search component so we can switch the current event
const eventSearch = document.querySelector('event-search');
if(typeof eventSearch !== 'undefined') {
eventSearch.addEventListener('select', this.changeCurrentEvent);
}
}
async changeCurrentEvent(event) {
await this.fetchTickets(event.id);
this.currentEvent = event;
}
async fetchTickets(eventId: number) {
try {
this.tickets = await fetchData(ticketEndpoint(eventId))
} catch (error) {}
}
However, when executing the event i get the following error:
Uncaught (in promise) TypeError: this.fetchTickets is not a function at HTMLElement.<anonymous>
Apparently it does land in the changeCurrentEvent
function but it cannot find the fetchTickets
function.
You can use ES6 Arrow Function syntax to avoid having to write this.changeCurrentEvent.bind(this)
.
changeCurrentEvent = async (event) => {
await this.fetchTickets(event.id);
this.currentEvent = event;
}