文章来源: Events (lwc.dev)

1. 监控子组件的动作:

  1. Child.html
  2.  
  3. <template>
  4. <button>Click</button>
  5.  
  6. </template>
  7.  
  8. Parent.html
  9.  
  10. <template>
  11.  
  12. <c-child></c-child>
  13.  
  14. </template>
  15.  
  16. Parent.JS
  17.  
  18. import { LightningElement } from 'lwc';
  19.  
  20. export default class App extends LightningElement {
  21. renderedCallback(){
  22. this.template.querySelector('c-child').addEventListener('click',
  23. this.handleClick);
  24. }
  25.  
  26. handleClick(e){
  27. // Your code here
  28. alert('This is come from p');
  29. }
  30. }

2. 普通的一个Action

  1. <div>
  2. <button onclick={handlePrevious}>Previous</button>
  3. <button onclick={handleNext} class="right">Next</button>
  4. </div>
  5.  
  6. handlePrevious() {
  7. this.dispatchEvent(new CustomEvent('previous'));
  8. alert('P')
  9. }
  10.  
  11. handleNext() {
  12. this.dispatchEvent(new CustomEvent('next'));
  13. alert('N')
  14. }

  

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.

  1. Child.html
  2. <template>
  3. <div>
  4. <button onclick={handlePrevious}>Previous</button>
  5. <button onclick={handleNext} class="right">Next</button>
  6. </div>
  7. </template>
  8.  
  9. child.js
  10. import { LightningElement } from "lwc";
  11.  
  12. /**
  13. * Show an item
  14. */
  15. export default class Child extends LightningElement {
  16. handlePrevious() {
  17. this.dispatchEvent(new CustomEvent('previous1'));
  18. }
  19.  
  20. handleNext() {
  21. this.dispatchEvent(new CustomEvent('next'));
  22. }
  23. }
  24.  
  25. 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. 今日实际操作----Dart Mac开发与运行环境配置 配置.bash_profile

    Mac 打开.编辑 .bash_profile 文件 一般在Mac上配置环境变量时经常要创建.编辑 .bash_profile文件.创建该文件时一般都会选择在当前用户目录下,即Mac下的.bash_p ...

  2. 前端防错以及好用小tips指南总结

    @前端防錯以及好用小tips指南總結 1.一般情況下我們接收到的都是對象格式,某些情況下,需要接到後端傳過來的奇怪的字符串格式的JSON,需要解析成對象,但是有時候他們傳過來的格式有問題,會報錯 解決 ...

  3. Python从零到壹丨图像增强及运算:图像掩膜直方图和HS直方图

    摘要:本章主要讲解图像直方图相关知识点,包括掩膜直方图和HS直方图,并通过直方图判断黑夜与白天,通过案例分享直方图的实际应用. 本文分享自华为云社区<[Python从零到壹] 五十二.图像增强及 ...

  4. 【Oculus Interaction SDK】(七)使用射线进行交互(物体 & UI)

    前言 这篇文章是[Oculus Interaction SDK]系列的一部分,如果发现有对不上的对方,可以回去翻看我之前发布的文章,或在评论区留言.如果文章的内容已经不适用于新版本了,也可以直接联系我 ...

  5. ES字段类型

    1 String ELasticsearch 5.X之后的字段类型不再支持string,由text和keyword取代,不做说明. 2 text和keyword 2.1 简介 ElasticSearc ...

  6. Html5 canvas创意特效合集

    Canvas就像一块画布,我们可以通过调用脚本在Canvas上绘制任意形状,甚至是制作动画.本文就是收集了很多非常富有创意的一些canvas动画特效例子,这些例子都非常适合大家学习.更多源码可在在这里 ...

  7. Python实现随机森林RF并对比自变量的重要性

      本文介绍在Python环境中,实现随机森林(Random Forest,RF)回归与各自变量重要性分析与排序的过程.   其中,关于基于MATLAB实现同样过程的代码与实战,大家可以点击查看MAT ...

  8. 学习Java Day29

    今天学习了类的设计技巧: 1.一定要保证数据私有 2.一定要对数据初始化 3.不要在类中使用过多的基本类型 4.不是所有的字段都需要单独的字段访问器和字段更改器 5.分解有过多职责的类 6.类名和方法 ...

  9. webpack核心用法,为什么要使用webpack

    一:为什么使用webpack 1. 代码转换.文件优化.代码分割.模块合并.自动刷新.等等 2. webpack上手 <!DOCTYPE html> <html lang=" ...

  10. 大规模 IoT 边缘容器集群管理的几种架构-1-Rancher+K3s

    前文回顾 大规模 IoT 边缘容器集群管理的几种架构-0-边缘容器及架构简介 ️Reference: IoT 边缘计算系列文章 Rancher + K3s 简介 Rancher: Kubernetes ...