JAVASCRIPT Tutorial
JavaScript-ramverk som React, Angular och Vue erbjuder inbyggda sätt att hantera händelser. Dessa ramverk förenklar processen genom att tillhandahålla:
I React använder du props-attributen onClick
, onChange
, etc. för att lägga till händelselyssnare.
Exempel:
import React from "react";
const MyComponent = () => {
const handleClick = () => {
console.log("Klick!");
}
return (
<button onClick={handleClick}>Klicka här</button>
);
};
export default MyComponent;
I Angular använder du direktivet (click)
för att lägga till händelselyssnare.
Exempel:
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
template: `<button (click)="handleClick()">Klicka</button>`
})
export class MyComponent {
handleClick() {
console.log("Klick!");
}
}
I Vue använder du v-on
-direktivet för att lägga till händelselyssnare.
Exempel:
import Vue from 'vue';
new Vue({
el: '#app',
methods: {
handleClick() {
console.log("Klick!");
}
},
template: `<button v-on:click="handleClick()">Klicka</button>`
});