How do I create a custom event in Typescript?

How to create customEvent Typescript and use it? I found this javascript link on the Mozilla site ( https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent )

I'm just testing a custom event, but Typescript treats it as a bug. what i am planning to do is add some additional data to the details property for later use: here is my code.

let div:any=document.getElementById("my_div");

let c_event = new CustomEvent("build",{'details':3 });

div.addEventListener("build",function(e:Event){
    console.log(e.details);
}.bind(this));

div.dispatchEvent(c_event);

      

+3


source to share


1 answer


The property name detail

, not details

. The correct code should be:



let div: any = document.getElementById("my_div");

let c_event = new CustomEvent("build",{detail: 3});

div.addEventListener("build", function(e: CustomEvent) { // change here Event to CustomEvent
    console.log(e.detail);
}.bind(this));

div.dispatchEvent(c_event);

      

+6


source







All Articles