How can we use publish-subscriber event pattern in Community Cloud using lightining web component.
I have drag-and-drop two Lightining Web Component(LWC) in a community page. On click on first component I have to show some output in second component.
Same components are working in Lightning Page(Like: Account detail page) but not in Community page. I used pubsub module from the lwc-receipe for this.
Code which fires the Event:
import { LightningElement, api, wire } from 'lwc';
import {CurrentPageReference} from 'lightning/navigation';
import { fireEvent } from 'c/pubsub'
export default class LwcAccounttile extends LightningElement {
@api name;
@wire(CurrentPageReference) pageRef;
tileClick() {
fireEvent(this.pageRef, 'inputChangeEvent', this.name);
}
}
Code which subscribe the events
import { LightningElement,track,api,wire } from 'lwc';
import { CurrentPageReference} from 'lightning/navigation';
import { registerListener, unregisterAllListeners, fireEvent } from 'c/pubsub';
export default class Lwc_AccountEventListner extends LightningElement {
@track ac;
@track inpVal;
@wire(CurrentPageReference) pageRef;
connectedCallback() {
// subscribe to inputChangeEvent event
registerListener('inputChangeEvent', this.handleChange, this);
}
disconnectedCallback() {
// unsubscribe from inputChangeEvent event
unregisterAllListeners(this);
}
handleChange(inpVal) {
this.inpVal = inpVal;
}
}
During debugging I found that this.pageRef is returning undefined in the commnunity while in lightning page it is returning value.