Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To bring in MouseEvent to Angular 9, you may follow these steps:

  1. Import "Component" and "ViewChild" from "@angular/core".
  2. Declare a reference to a native element in the component using "@ViewChild" decorator.
  3. Add an event listener function to the native element using "addEventListener" method.
  4. Pass in the "MouseEvent" object as a parameter to the event listener function.

Here is an example code snippet:

import { Component, ViewChild } from '@angular/core';

@Component({ selector: 'app-mouse-event', template: '<button #btn="">Click Me!</button>' }) export class MouseEventComponent {

@ViewChild('btn') btnRef: ElementRef;

constructor() { }

ngOnInit() { this.btnRef.nativeElement.addEventListener('click', this.onClick.bind(this)); }

onClick(event: MouseEvent) { console.log('Button clicked!', event); } }