文章来源: Events (lwc.dev)

1. 监控子组件的动作:

Child.html

<template>
<button>Click</button> </template> Parent.html <template> <c-child></c-child> </template> Parent.JS import { LightningElement } from 'lwc'; export default class App extends LightningElement {
renderedCallback(){
this.template.querySelector('c-child').addEventListener('click',
this.handleClick);
} handleClick(e){
// Your code here
alert('This is come from p');
}
}

2. 普通的一个Action

  <div>
<button onclick={handlePrevious}>Previous</button>
<button onclick={handleNext} class="right">Next</button>
</div> handlePrevious() {
this.dispatchEvent(new CustomEvent('previous'));
alert('P')
} handleNext() {
this.dispatchEvent(new CustomEvent('next'));
alert('N')
}

  

The CustomEvent() constructor has one required parameter, which is a string indicating the event type. As a component author, you name the event type when you create the event. The event type is the name of the event. You can use any string as your event type. However, we recommend that you conform with the DOM event standard.

  • No uppercase letters
  • No spaces
  • Use underscores to separate words

3.

Child.html
<template>
<div>
<button onclick={handlePrevious}>Previous</button>
<button onclick={handleNext} class="right">Next</button>
</div>
</template> child.js
import { LightningElement } from "lwc"; /**
* Show an item
*/
export default class Child extends LightningElement {
handlePrevious() {
this.dispatchEvent(new CustomEvent('previous1'));
} handleNext() {
this.dispatchEvent(new CustomEvent('next'));
}
} parent

  4. child pass Data to Parent

参照Recipes上的例子:lwc-recipes/force-app/main/default/lwc/contactListItem at main · trailheadapps/lwc-recipes · GitHub

lwc-recipes/eventWithData.html at main · trailheadapps/lwc-recipes · GitHub

另外实际项目上的代码

CreateFolder、FavoriteView
 
5. parent to child
 
 
6. 动态添加Option下拉选项值:
https://salesforce.stackexchange.com/questions/275530/dynamically-update-lightning-combobox-lightning-web-component 
https://developer.salesforce.com/forums/?id=9062I000000DHcLQAW 

LWC-001_Event的更多相关文章

  1. Salesforce LWC学习(二) helloWorld程序在VSCode中的实现

    上一篇我们简单的描述了一下Salesforce DX的配置以及CLI的简单功能使用,此篇主要简单描述一下LWC如何实现helloWorld以及LWC开发时应该注意的一些规范. 做国内项目的同学直观的感 ...

  2. Salesforce LWC学习(三) import & export / api & track

    我们使用vs code创建lwc 时,文件会默认生成包含 template作为头的html文件,包含了 import LightningElement的 js文件以及对应的.js-meta.xml文件 ...

  3. Salesforce LWC学习(四) 父子component交互 / component声明周期管理 / 事件处理

    我们在上篇介绍了 @track / @api的区别.在父子 component中,针对api类型的变量,如果声明以后就只允许在parent修改,son component修改便会导致报错. sonIt ...

  4. Salesforce LWC学习(五) LDS & Wire Service 实现和后台数据交互 & meta xml配置

    之前的几节都是基于前台变量进行相关的操作和学习,我们在项目中不可避免的需要获取数据以及进行DML操作.之前的内容中也有提到wire注解,今天就详细的介绍一下对数据进行查询以及DML操作以及Wire S ...

  5. Salesforce LWC学习(六) @salesforce & lightning/ui*Api Reference

    上一篇中我们在demo中使用了很多的 @salesforce 以及 lightning/ui*Api的方法,但是很多没有细节的展开.其实LWC中针对这些module提供了很多好用的方法,下面对这两种进 ...

  6. Salesforce LWC学习(七) Navigation & Toast

    上一篇我们介绍了针对LWC中常用的LDS的适配的wire service以及@salesforce模块提供的相关的service,其实LWC中还提供其他的好用的service,比如针对导航相关的lig ...

  7. trailhead学习之 LWC for Aura Developers

    本篇查看https://trailhead.salesforce.com/content/learn/modules/lightning-web-components-for-aura-develop ...

  8. Salesforce LWC学习(一)Salesforce DX配置

    LWC: Create a Salesforce DX Project and Lightning Web Component:https://www.youtube.com/watch?v=p268 ...

  9. Salesforce LWC学习(八) Look Up组件实现

    本篇参考https://www.salesforcelwc.in/2019/10/lookup-in-lwc.html,感谢前人种树. 我们做lightning的时候经常会遇到Look up 或者MD ...

  10. Salesforce LWC学习(九) Quick Action in LWC

    我们在lightning开发中,quick action是一个常用的功能,很可惜的是,lwc目前还不支持单独的custom quick action操作,只能嵌套在aura中使用才能发挥作用. 官方也 ...

随机推荐

  1. DateFormat类&SimpleDateFrormat类介绍-Dateformat类的format方法parse方法

    DateFormat类&SimpleDateFrormat类介绍 java.text.DateFormat是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就 ...

  2. MyBatis的使用三(在sql语句中传值)

    本文主要介绍在mybatis中如何在sql语句中传递参数 一. #{ } 和 ${ } 1. #{ } 和 ${ }的区别 #{ }是预编译处理 ==> PreparedStatement ${ ...

  3. 【MRTK】HoloLens开发基础项目设置

    前言 好记性不如烂笔头,之前做项目的时候很熟练很顺手就没有写笔记.因为排期问题项目中断几个月之后需要重新拾起来,结果发现自己现在忘记得差不多了,于是还是决定写点东西记录一下.即便是简单的项目设置,忘记 ...

  4. KingbaseES数据库目录结构

    KingbaseES数据库目录结构 [kingbase@postgres V8]$ tree -LP 2 data/ . ├── data │ ├── base # 存储用户创建的数据库文件及隶属于用 ...

  5. Vue21 组件

    1 模块及组件简介 组件(component)是vue.js最强大的功能之一.组件的作用就是封装可重用的代码,通常一个组件就是一个功能体,便于在多个地方都能够调用这个功能体. 每个组件都是Vue的实例 ...

  6. springboot框架返回日期值少一天

    1 问题 一个请求,返回一个对象,对象里面有日期,返回的对象的日期却比实际的日期少了一天 如下图:在return返回的时候查看返回的对象的日期的值 postman返回的结果如下图,几个日期都少了一天 ...

  7. cannot load "mso.dll" vs2008 web开发问题

    已成功解决办法: ①将VS 2008安装包WCUWebDesignerCoreWebDesignerCore.exe提取并重新安装: ②将C:Program Files/Common Files/Mi ...

  8. TIM + DMA + ADC

    #include "public.h" #define FFT_POINT (256) static uint16_t _DmaBuffer[FFT_POINT]; static ...

  9. ctfshow_web入门 sql注入(web171~248)

    sql注入 这是算是学习+做题+记录的一个笔记吧,而且基本都是看着Y4师傅的博客做的 由于是做过sqli靶场,所以这个就记录快点了.如果靶场没遇到的,也会做笔记. union 联合注入 web171 ...

  10. 前后端分离项目创建项目详细过程项、目需求分析、pip换源、创建虚环境、后端目录调整以及解决问题

    引言,本项目是前后端分离的,前端用Vue2 后端用Django,后台管理部分是通过simpleUI完成的项目,项目名称为路飞,是商城类(知识付费项目).本篇文章主要讨论一个前后端分离的项目第一步怎么做 ...