JAVASCRIPT Tutorial
React:
addEventListener()
or onClick
attribute.<button onClick={handleClick}>Click Me</button>
Angular:
Event Emitter
service.this.eventEmitter.emit('click', {message: 'Clicked!'})
.Vue:
v-on
directive.<button v-on:click="handleClick">Click Me</button>
Frameworks simplify event handling by:
// Vanilla JavaScript
document.querySelector('button').addEventListener('click', () => {
console.log('Button clicked!');
});
// React
const handleClick = () => {
console.log('Button clicked!');
};
const App = () => {
return <button onClick={handleClick}>Click Me</button>;
};
// Angular
import {EventEmitter} from '@angular/core';
...
this.eventEmitter = new EventEmitter();
...
this.eventEmitter.emit('click', {message: 'Clicked!'});
// Vue
<template>
<button @click="handleClick">Click Me</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked!');
}
}
};
</script>
Event listeners play a crucial role in interactive web applications. JavaScript frameworks enhance event handling by providing simplified APIs, automatation, and browser compatibility support. Understanding how event listeners work in frameworks is essential for developing efficient and responsive user interfaces.