How can you use register Listener?

You can use register listener in following ways:


childcomponent.js

import { LightningElement, wire } from ‘lwc’;

import { fireEvent, registerListener } from ‘c/pubsub’;

import { CurrentPageReference } from ‘lightning/navigation’;

export default class childcomponent extends LightningElement {

@wire(CurrentPageReference) pageRef;  

removedata(event){

fireEvent(this.pageRef, ‘removeElement’, {

“details”: event.target.dataset

});

}

}

childcomponent.html

<template>

<template for:each={record} for:item=”item” for:index=”indexval”>

<div data-indexval={indexval} onclick={removedata}> {record.name} </div>

</template>

</template>

parentcomponent.html

<template>

<c-child-component></c-child-component>

</template>

parentcomponent.js

import { LightningElement, track } from ‘lwc’;

import { registerListener } from ‘c/pubsub’;

export default class parentcomponent extends LightningElement {

@track datavalue= [];

connectedcallback(){

registerListener(‘removeElement’, this.removeElement, this);

}

removeElement(data) {                               

if (data.details.indexval !== undefined && data.details.indexval !== null) {

this.datavalue.splice(parseInt(data.details.indexval), 1);

}                           

}

}

About the author

By Rohit

Recent Posts

Recent Comments

Archives

>