I am adding an LWC component in visualforce and need to add a handler for an event fired in the LWC component on the visualforce page. When googling all I can find is examples of how to do this with aura component using application events.
LWC controller:
handleSearchClick() {
const searchinfo = {
shipper: this.shipper,
system: this.system,
product: this.product,
status: this.template.querySelector('c-lwc-multi-select').value(),
period: this.period
};
this.dispatchEvent(new CustomEvent(
'dosearch',
{
detail: { data: searchinfo},
bubbles: true,
composed: true,
}
));
console.log({ 'lwcNominationSearch searchinfo': searchinfo });
}
AURA app:
<aura:application extends="ltng:outApp" access="GLOBAL">
<aura:dependency resource="c:lwcNominationSearch" />
<aura:dependency resource="markup://force:*" type="EVENT"/>
</aura:application>
Visualforce:
<div id="rcontainer">
</div>
<script>
$Lightning.use("c:NotificationSearch_ltngoutApp", function()
{
$Lightning.createComponent(
"c:lwcNominationSearch",
{},
"rcontainer",
function(cmp)
{
console.log('component created');
//$A.eventService.addHandler({ "event": "c:myEvent", "handler" : visualForceFunction})
$A.eventService.addHandler({
event: 'dosearch',
handler: function (event) {
console.log("vf event success");
}
});
});
});
</script>