You can use a pubsub emitter/dispatcher pattern. I call this LWC a messageBroker
. You can find a working example of LWC and Aura interoperability in my repo here
Basically, to have uni (even bi-directional) data move from LWC to aura, right now the best technique is to place an Aura wrapper somewhere on your flexipage.
With a standardized LWC pub-sub channel messageBroker
(or whatever you want), the LWC parts are as follows:
lwcRequestor.js
fireEvent(this.pageRef, 'messageBroker', payload});
// payload.data
// payload.recordId, etc
messageBroker.js
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
import { registerListener, unregisterAllListeners } from 'c/pubsub';
export default class MessageBroker extends LightningElement {
@wire(CurrentPageReference) pageRef;
connectedCallback() {
registerListener('messageBroker', this.messageBrokerEmitter, this);
}
disconnectedCallback() {
unregisterAllListeners(this);
}
messageBrokerEmitter(payload) {
// Just pass this on through to the parent enclosing aura component
this.dispatchEvent(new CustomEvent('message', { detail: { payload } }));
}
}
Then the aura component below placed on your flexipage. This is important because of the current limitations of the lwc-recipe pubsub component requires @wire pageRef
. (Though you can strip that check out if you want, and swap for say - a recordId).
This aura wrapper is as such:
MessageBrokerAuraHandler.cmp
<aura:component implements="flexipage:availableForAllPageTypes">
<c:messageBroker onmessage="{! c.handleMessageBroker }"></c:messageBroker>
</aura:component>
Then you're freely able to handle the LWC's payload in your aura component via c.handleMessageBroker
.
Best Answer
Yes you should be able to do this by wrapping up the LWC base component in a custom LWC Component. You can use custom LWC component in an Aura Component. Use this link to understand how to use custom LWC in aura component: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.interop_aura_composition This way you should be able to use benifit of LWC with existing AURA Component.