Lightning Web Component: Event (Parent to Chlid / Child to Partner)

Events- Parent To Child Navigation

Sometimes it is needed to merge the two or more components and after merging we also need a data flow between the components.

To pass data from top-bottom (Parent to child) in the component list, the child or lower component must declare a public API, with this public API parent can pass the data to child

Public properties

Public properties are properties denoted by the @api decorator. It is also known as Public reactive property.

Note: It is crucial that You can assign a default value to public property, but you should never change the value of public property in the component.

Events- Child To Parent Navigation

Data must be passed up using Events. Events are the activity or the action that fires from the child component and listens to the parent.

LWC creates the Events using the Event or CustomEvent standard JavaScript classes. The CustomEvent class will allow you to store information in its detail property and then transmit those details(information) to the listeners of the event. Then, you can make an event target dispatch to invoking the dispatchEvent standard method.

Event propagation

Event propagation means launching a series of events in the web browser. This can be the top to bottom and bottom to top. Event propagation defines how to travel in the DOM tree.

There are two types of propagation that manage the Lightning component event handling.

  • Bubble: Bubble event allow the event to fire upward direction
  • Composed: composed determines whether the event should ignore the shadow DOM or not.
  • Syntax:
    this.dispatchEvent(new CustomEvent('clickevevent', { bubbles: false , composed : false }));

Practical operation

LWC: Parent Component

HTML

html 复制代码
<template>


    <div if:true={isMainPage}>
        <p>This is parent page.</p>
        <button class="slds-button slds-button_outline-brand" onclick={handleClick}>Go to Child Page</button>
    </div>
    <div if:true={isChildPage}>
        <c-child-Component onchildclick={handleChildEvent} message={message} ></c-child-Component>
    </div>


 </template>

JS

js 复制代码
import { LightningElement,api,track } from 'lwc';

export default class ParentComponent extends LightningElement {
    @track isMainPage = true;
    @track isChildPage = false;
    @api message;
    constructor(){
        super();
        this.template.addEventListener('childclick', this.handleClick.bind(this));
    }

    handleClick(event){
        this.message = 'Parent send data to child.';
        this.isMainPage = false;
        this.isChildPage = true;
    }

    handleChildEvent(event){
        this.isMainPage = event.detail.isMainPage;
        this.isChildPage = event.detail.isChildPage;
    }

}

LWC: Child Component

HTML

html 复制代码
<template>
    <div>
        <p>This is child page.</p>
        Message: {message}
        <button class="slds-button slds-button_outline-brand" onclick={clickbutton}>Go to Parent Page</button>
    </div>
 </template>

JS

js 复制代码
import { LightningElement, api } from 'lwc';

export default class ChildComponent extends LightningElement {

    @api message;

    clickbutton(){
        const event = new CustomEvent('childclick', {
            detail: {isMainPage:true,isChildPage:false}
            });
        this.dispatchEvent(event);
    }
}

Picture

相关推荐
elangyipi1238 分钟前
前端面试题:如何减少页面重绘跟重排
前端·面试·html
想学后端的前端工程师15 分钟前
【前端安全防护实战指南:从XSS到CSRF全面防御】
前端·安全·xss
czlczl2002092519 分钟前
基于 Spring Boot 权限管理 RBAC 模型
前端·javascript·spring boot
未来之窗软件服务20 分钟前
幽冥大陆(六十七) PHP5.x SSL 文字加密—东方仙盟古法结界
服务器·前端·ssl·仙盟创梦ide·东方仙盟
小北方城市网31 分钟前
第 10 课:Node.js 后端企业级进阶 —— 任务管理系统后端优化与功能增强(续)
大数据·前端·vue.js·ai·性能优化·node.js
华仔啊32 分钟前
JavaScript 有哪些数据类型?它们在内存里是怎么存的?
前端·javascript
我有一棵树36 分钟前
淘宝 npm 镜像与 CDN 加速链路解析:不只是 Registry,更是分层静态加速架构
前端·架构·npm
zhousenshan37 分钟前
vue3基础知识100问
前端·vue.js
异界蜉蝣39 分钟前
Proxy vs Object.defineProperty:Vue3响应式原理的深度革命
开发语言·前端·javascript
前端早间课42 分钟前
Vue3路由实战:优雅封装+灵活拦截,解锁路由配置新姿势
前端·javascript·vue.js