stenciljs 可以方便的构建交互式组件
支持以下装饰器

  • component
  • prop
  • watch
  • state
  • method
  • element

component 说明

component 包含tag styleUrl 参数
tag 定义组件的名称,注意需要使用-连接, styleUrl 指定组件的样式

参考格式:
import { Component } from '@stencil/core'; @Component({
tag: 'todo-list',
styleUrl: 'todo-list.css'
})
export class TodoList {
...
}

prop 说明

定义组件的自定义属性,可以方便的进行组件间数据的传递
支持的数据类型有number string boolean Object array,可以
使用this 进行数据访问,在html 设置需要使用dash-case 方式
在jsx 中使用camelCase 方式,默认prop 是不可变的,使用添加
mutable: true 进行修改,同时我们可以设置默认值

参考:
import { Prop } from '@stencil/core'; ...
export class TodoList {
@Prop() color: string;
@Prop() favoriteNumber: number;
@Prop() isSelected: boolean;
@Prop() myHttpService: MyHttpService;
} this 调用 logColor() {
console.log(this.color)
} html 设置属性 <todo-list color="blue" favorite-number="24" is-selected="true"></todo-list> jsx 设置属性 <todo-list color="blue" favoriteNumber="24" isSelected="true"></todo-list>
设置默认值
@Prop({ mutable: true }) name: string = 'Stencil';

watch 说明

我们可以使用watch 进行prop 值验证处理,包含新、旧数据

参考:
import { Prop, Watch } from '@stencil/core';
...
export class LoadingIndicator {
@Prop() activated: boolean; @Watch('activated')
watchHandler(newValue: boolean, oldValue: boolean) {
console.log('The new value of activated is: ', newValue);
}
}

state 说明

state 可以进行组件内部状态的管理,state 的更新会造成render 再次调用组件
函数

import { State } from '@stencil/core';

...
export class TodoList {
@State() completedTodos: Todo[]; completeTodo(todo: Todo) {
// This will cause our render function to be called again
this.completedTodos = [...this.completedTodos, todo];
} render() {
//
}
}

method 说明

可以方便的导出函数,方便外部调用

参考:
import { Method } from '@stencil/core'; ...
export class TodoList { @Method()
showPrompt() {
// show a prompt
}
} 调用:
const todoListElement = document.querySelector('todo-list');
todoListElement.showPrompt();

Element 说明

可以在class中访问当前组件,返回一个HTMLElement,可以使用DOM方式以及
事件

import { Element } from '@stencil/core';

...
export class TodoList {
@Element() todoListEl: HTMLElement; addClass(){
this.todoListEl.classList.add('active');
}
}

嵌入&&嵌套组件

参考:
import { Component, Prop } from '@stencil/core'; @Component({
tag: 'my-embedded-component'
})
export class MyEmbeddedComponent {
@Prop() color: string = 'blue'; render() {
return (
<div>My favorite color is {this.color}</div>
);
}
} import { Component } from '@stencil/core'; @Component({
tag: 'my-parent-component'
})
export class MyParentComponent { render() {
return (
<div>
<my-embedded-component color="red"></my-embedded-component>
</div>
);
}
}

参考资料

https://stenciljs.com/docs/decorators

 
 
 
 

stenciljs 学习四 组件装饰器的更多相关文章

  1. TypeScript学习笔记(四)装饰器

    目录 一.装饰器的作用 二.类装饰器 1. 普通装饰器 为类扩展属性和方法 使用装饰器修改属性和重写方法 2. 装饰器工厂 三.属性装饰器 四.方法装饰器 使用方法装饰器对方法进行扩展 五.方法参数装 ...

  2. python学习笔记之装饰器、递归、算法(第四天)

    参考老师的博客: 金角:http://www.cnblogs.com/alex3714/articles/5161349.html 银角:http://www.cnblogs.com/wupeiqi/ ...

  3. stenciljs 学习六 组件开发样式指南

    组件不是动作,最好使用名词而不是动词, 文件结构 每个文件一个组件. 每个目录一个组件.虽然将类似的组件分组到同一目录中可能是有意义的,但我们发现当每个组件都有自己的目录时,更容易记录组件. 实现(. ...

  4. Python小白学习之函数装饰器

    装饰器 2018-10-25 13:49:37 装饰器从字面意思就是用来装饰的,在函数可以理解为:在函数中,我们不想影响原来的函数功能,又想给函数添加新的功能,这时候我们就用到了装饰器. 一般函数操作 ...

  5. Python学习笔记012——装饰器

    1 装饰器 1.1装饰器定义 在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 1.2 装饰器分类 装饰器:函数装饰器,类装饰器,函数的装饰器,类的装饰器 装饰器:函数装饰函 ...

  6. python学习笔记(五):装饰器、生成器、内置函数、json

    一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里 ...

  7. Python学习笔记:装饰器

    Python 装饰器的基本概念和应用 代码编写要遵循开放封闭原则,虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被扩展,即: 封闭:已 ...

  8. python学习之类的装饰器进阶版

    装饰器可以修饰函数,同样,也可以修饰类 装饰器 def deco(func):    print('======>被修饰的')return func 装饰器装饰函数的方式,语法糖 @decode ...

  9. Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化

    本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解 ...

随机推荐

  1. 20170706wdVBA保存图片到本地API

    Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type Pri ...

  2. Confluence 6 快捷键

    快捷键图标. 官方的下载地址为:https://atlassianblog.wpengine.com/wp-content/uploads/2018/01/keyboard-shortcuts-inf ...

  3. Knights of a Polygonal Table CodeForces - 994B (贪心)

    大意:n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数 按战力排序后, 堆维护动态前k大即可 #include <iostre ...

  4. bzoj2242: [SDOI2011]计算器 BSGS+exgcd

    你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值:(快速幂) 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数:(exgcd) 3.给 ...

  5. C++虚函数与多态

    C++多态性是通过虚函数来实现的,虚函数允许子类重新定义成员函数,而子类重新定义父类的做法称为覆盖(override),或者称为重写.(这里我觉得要补充,重写的话可以有两种,直接重写成员函数和重写虚函 ...

  6. 最新的ES 5.0路由算法底层实现

    http://www.cnblogs.com/bonelee/p/6078947.html 里分析了ES bulk实现,其中路由代码: ShardId shardId = clusterService ...

  7. Mysql 时间类型整理

    一.date_sub.SUBDATE.date_add select now(),  date_sub(now(),interval  1 minute),SUBDATE(now(),interval ...

  8. DevExpress v17.2新版亮点——CodeRush篇(三)

    用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了CodeRush v17.2 的新功能,快来下载试用新版本! 代码格式和清理 文档 ...

  9. Final阶段第1周/共1周 Scrum立会报告+燃尽图 01

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  10. ASIHTTPRequest缓存策略download cache

    本文为大家介绍了iOS开发ASIHTTPRequest使用download cache的内容,其中包括cache策略,存储策略,其他cache相关的特性,编写自己的cache等等内容. 从1.8版本开 ...