LWC-001_Event
文章来源: 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
另外实际项目上的代码
LWC-001_Event的更多相关文章
- Salesforce LWC学习(二) helloWorld程序在VSCode中的实现
上一篇我们简单的描述了一下Salesforce DX的配置以及CLI的简单功能使用,此篇主要简单描述一下LWC如何实现helloWorld以及LWC开发时应该注意的一些规范. 做国内项目的同学直观的感 ...
- Salesforce LWC学习(三) import & export / api & track
我们使用vs code创建lwc 时,文件会默认生成包含 template作为头的html文件,包含了 import LightningElement的 js文件以及对应的.js-meta.xml文件 ...
- Salesforce LWC学习(四) 父子component交互 / component声明周期管理 / 事件处理
我们在上篇介绍了 @track / @api的区别.在父子 component中,针对api类型的变量,如果声明以后就只允许在parent修改,son component修改便会导致报错. sonIt ...
- Salesforce LWC学习(五) LDS & Wire Service 实现和后台数据交互 & meta xml配置
之前的几节都是基于前台变量进行相关的操作和学习,我们在项目中不可避免的需要获取数据以及进行DML操作.之前的内容中也有提到wire注解,今天就详细的介绍一下对数据进行查询以及DML操作以及Wire S ...
- Salesforce LWC学习(六) @salesforce & lightning/ui*Api Reference
上一篇中我们在demo中使用了很多的 @salesforce 以及 lightning/ui*Api的方法,但是很多没有细节的展开.其实LWC中针对这些module提供了很多好用的方法,下面对这两种进 ...
- Salesforce LWC学习(七) Navigation & Toast
上一篇我们介绍了针对LWC中常用的LDS的适配的wire service以及@salesforce模块提供的相关的service,其实LWC中还提供其他的好用的service,比如针对导航相关的lig ...
- trailhead学习之 LWC for Aura Developers
本篇查看https://trailhead.salesforce.com/content/learn/modules/lightning-web-components-for-aura-develop ...
- Salesforce LWC学习(一)Salesforce DX配置
LWC: Create a Salesforce DX Project and Lightning Web Component:https://www.youtube.com/watch?v=p268 ...
- Salesforce LWC学习(八) Look Up组件实现
本篇参考https://www.salesforcelwc.in/2019/10/lookup-in-lwc.html,感谢前人种树. 我们做lightning的时候经常会遇到Look up 或者MD ...
- Salesforce LWC学习(九) Quick Action in LWC
我们在lightning开发中,quick action是一个常用的功能,很可惜的是,lwc目前还不支持单独的custom quick action操作,只能嵌套在aura中使用才能发挥作用. 官方也 ...
随机推荐
- (18)go-micro微服务ELK介绍
目录 一 什么是ELK 二 Beats的六种工具 三 ELK系统的特点 四 ELK+beats系统架构 五 ELK优点 六 最后 一 什么是ELK ELK是三个[开源软件]的缩写,分别表示:Elast ...
- while循環的一些判斷條件和讀取循環精簡方法
while是入口條件循環 在進入循環體時必須要有獲取輸入數據并判斷的并檢查輸入數據的值,所以在while之前放一個scanf函數,讓循環繼續執行. 如果想讓循環繼續進行,那麽還需要在循環内設置一個讀取 ...
- ionic+vue+capacitor系列笔记--常见报错以及解决
1.Require statement not part of import statement.(@typescript-eslint/no-var-requires) 解决 .eslintrc.j ...
- Docker安装与卸载(基本命令)
Dockers的安装搭建 参考: https://www.cnblogs.com/jxxiaocao/p/12069139.html 采用apt源安装Docker的其他组件时,新组件与已安装的Dock ...
- 【随笔记】NDK 编译开源库 ffmpeg
一.下载源代码 wget http://ffmpeg.org/releases/ffmpeg-4.4.tar.gz tar -zxvf ffmpeg-4.4.tar.gz 二.编译配置脚本 #! /b ...
- Nacos配置中心 (介绍与配置)
Nacos配置中心 当微服务部署的实例越来越多,达到数十.数百时,逐个修改微服务配置就会让人抓狂,而且很容易出错.我们需要一种统一配置管理方案,可以集中管理所有实例的配置. Nacos一方面可以将配置 ...
- 剑指 Offer 32 - I. 从上到下打印二叉树(java解题)
目录 1. 题目 2. 解题思路 3. 数据类型功能函数总结 4. java代码 1. 题目 从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印. 例如: 给定二叉树: [3,9, ...
- mysql16 sql优化-order by-GROUP BY
1.简要说明 在sql中常常用到order by,排序对于sql的查询速度有较大的的影响.mysql支持两种排序方式,FileSort和Index,Index效率高,它指MySQL扫描索引本身完成 ...
- CSS特效集锦(9款 , 总有一款是你喜欢的)
主要是: 穿越时空特效, 图片放大镜, 3D相册, 立方体相册, 昼夜更替特效, 飘雪, 七彩雨, 签名生成器, 水波纹动画等 代码部分 <section> <div class=& ...
- SpringCloud Stream消息驱动
简单搭建,没有技术含量,Demo可用 1.介绍 ①产生原因 RabbitMQ.RocketMQ.Kafka.ActiveMQ 在一个项目中,可能存在多种不同的MQ,在不同的MQ中,切换维护开发都很麻烦 ...