StencilJs学习之组件装饰器
stenciljs 可以方便的构建交互式组件
支持以下装饰器
- component
- state
- prop
- watch
- method
- element
- event
- listen
Component 装饰器
@Component 是一个装饰器,它将 TypeScript 类指定为 Stencil 组件。 每个模板组件在构建时都会转换为 Web component。
import { Component } from '@stencil/core';
@Component({
tag: 'todo-list',
styleUrl: './todo-list.css',
// additional options
})
export class TodoList {
// implementation omitted
}
@Component装饰器还有其它的一些参数
- assetsDirs: 是从组件到包含组件所需的静态文件(资产)的目录的相对路径数组。
- scope:是否隔离css的作用域,如果启用了
shadow则此项不能设置为true。 - shadow: 阴影dom用来隔离样式。
- styleUrls:包含要应用于组件的样式的外部样式表的相对 URL 列表。
- styles:内联 CSS 而不是使用外部样式表的字符串。
State
@State 用于内部的状态管理,修改 @State装饰的变量会触发组件的重新渲染
import { Component, State, h } from '@stencil/core';
@Component({
tag: 'current-time',
})
export class CurrentTime {
timer: number;
// `currentTime` is decorated with `@State()`,
// as we need to trigger a rerender when its
// value changes to show the latest time
@State() currentTime: number = Date.now();
connectedCallback() {
this.timer = window.setInterval(() => {
// the assignment to `this.currentTime`
// will trigger a re-render
this.currentTime = Date.now();
}, 1000);
}
disconnectedCallback() {
window.clearInterval(this.timer);
}
render() {
const time = new Date(this.currentTime).toLocaleTimeString();
return (
<span>{time}</span>
);
}
}
Prop
@Prop 是用于声明外部数据传入组件的装饰器。
支持的数据类型有 number string boolean Object array,可以
使用this 进行数据访问,在html 设置需要使用dash-case 方式
在jsx 中使用camelCase 方式,默认prop 是不可变的,使用添加
mutable: true 进行修改, 使用 reflech 可以保持 prop 和 html属性 同步
import { Component, Prop, h } from '@stencil/core';
@Component({
tag: 'todo-list-item',
})
export class ToDoListItem {
@Prop({
mutable: true,
reflect: false
}) isComplete: boolean = false;
@Prop({ reflect: true }) timesCompletedInPast: number = 2;
@Prop({ reflect: true }) thingToDo: string = "Read Reflect Section of Stencil Docs";
}
Watch
@Watch()是应用于模具组件方法的修饰器。 修饰器接受单个参数,即用 @Prop() 或 @State() 修饰的类成员的名称。 用 @Watch() 修饰的方法将在其关联的类成员更改时自动运行。
// We import Prop & State to show how `@Watch()` can be used on
// class members decorated with either `@Prop()` or `@State()`
import { Component, Prop, State, Watch } from '@stencil/core';
@Component({
tag: 'loading-indicator'
})
export class LoadingIndicator {
// We decorate a class member with @Prop() so that we
// can apply @Watch()
@Prop() activated: boolean;
// We decorate a class member with @State() so that we
// can apply @Watch()
@State() busy: boolean;
// Apply @Watch() for the component's `activated` member.
// Whenever `activated` changes, this method will fire.
@Watch('activated')
watchPropHandler(newValue: boolean, oldValue: boolean) {
console.log('The old value of activated is: ', oldValue);
console.log('The new value of activated is: ', newValue);
}
// Apply @Watch() for the component's `busy` member.
// Whenever `busy` changes, this method will fire.
@Watch('busy')
watchStateHandler(newValue: boolean, oldValue: boolean) {
console.log('The old value of busy is: ', oldValue);
console.log('The new value of busy is: ', newValue);
}
@Watch('activated')
@Watch('busy')
watchMultiple(newValue: boolean, oldValue: boolean, propName:string) {
console.log(`The new value of ${propName} is: `, newValue);
}
}
mehtod
可以方便的导出函数,方便外部调用。
import { Method } from '@stencil/core';
export class TodoList {
@Method()
async showPrompt() {
// show a prompt
}
}
// used registered
el.showPrompt();
Element
@Element 装饰器是如何访问类实例中的 host 元素。这将返回一个 HTMLElement 实例,因此可以在此处使用标准 DOM 方法/事件。
import { Element } from '@stencil/core';
...
export class TodoList {
@Element() el: HTMLElement;
getListHeight(): number {
return this.el.getBoundingClientRect().height;
}
}
其它
Event 和 Listen 装饰器将在下一节 事件 中讲解。
StencilJs学习之组件装饰器的更多相关文章
- stenciljs 学习四 组件装饰器
stenciljs 可以方便的构建交互式组件 支持以下装饰器 component prop watch state method element component 说明 component 包含ta ...
- stenciljs 学习六 组件开发样式指南
组件不是动作,最好使用名词而不是动词, 文件结构 每个文件一个组件. 每个目录一个组件.虽然将类似的组件分组到同一目录中可能是有意义的,但我们发现当每个组件都有自己的目录时,更容易记录组件. 实现(. ...
- Python学习笔记012——装饰器
1 装饰器 1.1装饰器定义 在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 1.2 装饰器分类 装饰器:函数装饰器,类装饰器,函数的装饰器,类的装饰器 装饰器:函数装饰函 ...
- Python小白学习之函数装饰器
装饰器 2018-10-25 13:49:37 装饰器从字面意思就是用来装饰的,在函数可以理解为:在函数中,我们不想影响原来的函数功能,又想给函数添加新的功能,这时候我们就用到了装饰器. 一般函数操作 ...
- Python学习笔记:装饰器
Python 装饰器的基本概念和应用 代码编写要遵循开放封闭原则,虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被扩展,即: 封闭:已 ...
- python学习之day5,装饰器,生成器,迭代器,json,pickle
1.装饰器 import os import time def auth(type): def timeer(func): def inner(*args,**kwargs): start = tim ...
- python学习笔记之装饰器、递归、算法(第四天)
参考老师的博客: 金角:http://www.cnblogs.com/alex3714/articles/5161349.html 银角:http://www.cnblogs.com/wupeiqi/ ...
- python学习-day20、装饰器【图片缺失可看】印象笔记博客备份
前言: 装饰器用于装饰某些函数或者方法,或者类.可以在函数执行之前或者执行之后,执行一些自定义的操作. 1.定义:装饰器就是一个函数,为新定义的函数.把原函数嵌套到新函数里面.以后就可以在执行新函数的 ...
- Python学习之路——装饰器
开放封闭原则:不改变调用方式与源代码上增加功能 ''' 1.不能修改被装饰对象(函数)的源代码(封闭) 2.不能更改被修饰对象(函数)的调用方式,且能达到增加功能的效果(开放) ''' 装饰器 # 把 ...
- python学习日记(装饰器的补充)
如何返回被装饰函数的函数名及注释? 问题及实现 先看典型的装饰器: def wrapper(f):#装饰器函数,f是被装饰函数 def inner(*args,**kwargs): '''执行函数之前 ...
随机推荐
- 一文总结你需要的OpenCV操作
目录 一.OpenCV简介 1.1 OpenCV是什么 1.2 安装及使用 二.图像的基础 2.1 成像原理 2.2 图像格式 2.3 颜色空间 三.OpenCV基础操作 3.1 图像的读取.显示.保 ...
- 【SpringCloud】(一)分布式理论
分布式架构理论 方法远程调用 各个模块运行于不同的tomcat,模块之间通过网络进行调用. 远程调用的技术演进 1 WebService 解决应用程序之间的跨平台访问问题,基于SOAP/WSDL协议, ...
- 从原理聊JVM(二):从串行收集器到分区收集开创者G1
作者:京东科技 康志兴 1 前言 随着Java的进化过程,涌现出各种不同的垃圾回收器,从串行执行到并行执行,从高吞吐到低延迟,终极目标就是让开发人员专注于程序的代码书写而无需关注内存管理. JDK早期 ...
- CS144 计算机网络 Lab3:TCP Sender
前言 在 Lab2 中我们实现了 TCP Receiver,负责在收到报文段之后将数据写入重组器中,并回复给发送方确认应答号.在 Lab3 中,我们将实现 TCP 连接的另一个端点--发送方,负责读取 ...
- pytest常用断言
如上官网所说,pytest使用python中的assert来比较实际值与预期值,实现断言功能 1 常用的断言方式 assert xxx assert a == b assert a!=b assert ...
- 2021-02-07:给定两棵二叉树的头节点head1和head2,如何判断head1中是否有某个子树的结构和head2完全一样?
福哥答案2021-02-07: 对head1和head2序列化为str1和str2.然后用kmp算法去判断str2是否是str1的子串.如果是,head2是子树:如果不是,head2不是子树. 代码用 ...
- 2021-12-22:回文子串。 给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目。 回文字符串 是正着读和倒过来读一样的字符串。 子字符串 是字符串中的由连续字符组成的一个序列。
2021-12-22:回文子串. 给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目. 回文字符串 是正着读和倒过来读一样的字符串. 子字符串 是字符串中的由连续字符组成的一个序列. ...
- 2021-06-25:只由小写字母(a~z)组成的一批字符串,都放在字符类型的数组String[] arr中,如果其中某两个字符串所含有的字符种类完全一样,就将两个字符串算作一类,比如:baacbba
2021-06-25:只由小写字母(a~z)组成的一批字符串,都放在字符类型的数组String[] arr中,如果其中某两个字符串所含有的字符种类完全一样,就将两个字符串算作一类,比如:baacbba ...
- DataGridView数据内容自适应列宽
数据自适应宽度某一列dataGridView1.Columns[@"列名"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCe ...
- 源端为备库的场景下Duplicate失败问题
环境: Oracle 11.2.0.3 + OEL 7.9 A -> B -> C 级联ADG环境:db11g -> db11gadg -> db11gcas 之前测试提到,从 ...