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中使用才能发挥作用. 官方也 ...
随机推荐
- mysql常用命令,检查数据库连接情况以及修改时区
常用操作 注:也可以运行 mysql -u 用户名(root) -p 密码(root) 数据库名(bank) ,然后回车 导入文件:source e:bank.sql (你的sql文件) 回车 PS ...
- Unity之GPS定位(腾讯sdk)
Unity之GPS定位(腾讯sdk) 目录 Unity之GPS定位(腾讯sdk) 前言 Unity版本及使用插件 正题 编写脚本 Run运行, 跑起来就行,具体要什么,去相关类找就好了. 没有腾讯地图 ...
- immutable.js 学习笔记(三)----- Map
一.Map Map在原生的js中对应的是Object这样的结构,它都是key-value的键值对,并且它是无序的 二. API (一) set:设定值 (二)delete:删除值 每做一次增删改查都会 ...
- python学习第五周总结
面向对象前戏之人狗大战 # 编写代码简单的实现人打狗 狗咬人的小游戏(剧情需要) """推导步骤1:代码定义出人和狗""" person1 ...
- Redis-03 数据格式和基础命令
1 单进程 Redis采用单进程模型来处理客户端的请求.对读写等时间的响应是通过对epoll函数的包装来做到的.Redis的实际处理速度完全依靠主进程的执行效率. Epoll是Linux内核为 ...
- Spring 01 统一资源加载策略 Resource和ResourceLoader
转:https://www.cnblogs.com/loveLands/articles/10797772.html 1 Resource统一资源 1.1 简介 处理外部资源是很繁琐的事情,我们可能需 ...
- C#支付宝用户的静默授权
支付宝官方的用户信息授权,具体地址:https://opendocs.alipay.com/open/289/105656 商户/开发者通过以下的 URL 拼接规则拼接用户授权的 URL 地址,该地址 ...
- Eureka 注册中心和服务提供者
什么是Eureka组件 spring cloud Eureka ,提供服务注册和服务发现的功能. 一:spring cloud Eureka Eureka Server 服务端 Eureka Clie ...
- C#后缀表达式解析计算字符串公式
当我们拿到一个字符串比如:20+31*(100+1)的时候用口算就能算出结果为3151,因为这是中缀表达式对于人类的思维很简单,但是对于计算机就比较复杂了.相对的后缀表达式适合计算机进行计算. 我们就 ...
- MySQL索引的基本理解
之前一致以为索引就是简单的在原表的数据上加了一些编号,让查询更加快捷.后来发现里面还有更深的知识. 索引用于快速查找具有特定列值的行.如果没有索引,MySQL 必须从第一行开始,然后通读整个表以找到相 ...