可以使用jsx 方便组件的开发

基本格式

主要是render 函数

class MyComponent {
render() {
return (
<div>
<h1>Hello World</h1>
<p>This is JSX!</p>
</div>
);
}
}

数据绑定

render() {
return (
<div>Hello {this.name}</div>
)
}

条件语句

render() {
if (this.name) {
return ( <div>Hello {this.name}</div> )
} else {
return ( <div>Hello, World</div> )
}
}

slots

参考

render() {
return (
<div>
<h2>A Component</h2>
<div><slot /></div>
</div>
);
}

对于多个可以指定名称
参考

render(){
return [
<slot name="item-start" />,
<h1>Here is my main content</h1>,
<slot name="item-end" />
]
} render(){
return(
<my-component>
<p slot="item-start">I'll be placed before the h1</p>
<p slot="item-end">I'll be placed after the h1</p>
</my-component>
)
}

loops 操作

参考

render() {
return (
<div>
{this.todos.map((todo) =>
<div>
<div>{todo.taskName}</div>
<div>{todo.isCompleted}</div>
</div>
)}
</div>
)
}

处理用户输入事件

使用原生dom 事件
参考

export class MyComponent {
handleClick(event: UIEvent) {
alert('Received the button click!');
} render() {
return (
<button onClick={ (event: UIEvent) => this.handleClick(event)}>Click Me!</button>
);
}
}

获取dom 元素的引用

使用ref 进行参数绑定
参考

@Component({
tag: 'app-home',
})
export class AppHome{ textInput: HTMLInputElement; handleSubmit = (ev: Event) => {
ev.preventDefault();
console.log(this.textInput.value);
} render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" ref={(el: HTMLInputElement) => this.textInput = el} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

参考资料

https://stenciljs.com/docs/templating-jsx

 
 
 
 

stenciljs 学习九 使用jsx的更多相关文章

  1. stenciljs 学习五 事件

    组件可以使用Event Emitter装饰器发送数据和事件. Event 定义 参考: import { Event, EventEmitter } from '@stencil/core'; ... ...

  2. stenciljs 学习四 组件装饰器

    stenciljs 可以方便的构建交互式组件 支持以下装饰器 component prop watch state method element component 说明 component 包含ta ...

  3. React.js学习之理解JSX和组件

    在开启JSX的学习旅程前,我们先了解一下React的基本原理.React本质上是一个"状态机",它只关心两件事:更新DOM和响应事件,React不处理Ajax.路由和数据存储,也不 ...

  4. 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)

    layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...

  5. 【react学习笔记】-jsx

    //jsx定义组件 var Divider = React.creatClass({ getIsComplete:function(){ return 'is-complete' }, handleC ...

  6. ExtJS4.2学习(九)属性表格控件PropertyGrid(转)

    鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-15/178.html ------------- ...

  7. Windows核心编程学习九:利用内核对象进行线程同步

    注:源码为学习<Windows核心编程>的一些尝试,非原创.若能有助于一二访客,幸甚. 1.程序框架 #include "Queue.h" #include <t ...

  8. Java数据持久层框架 MyBatis之API学习九(SQL语句构建器详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  9. JVM学习九:JVM之GC算法和种类

    我们前面说到了JVM的常用的配置参数,其中就涉及了GC相关的知识,趁热打铁,我们今天就学习下GC的算法有哪些,种类又有哪些,让我们进一步的认识GC这个神奇的东西,帮助我们解决了C 一直挺头疼的内存回收 ...

随机推荐

  1. 20170709pptVBA递归删除LOGO图片与文字

    Public Sub StartRecursionFolder() Dim Pre As Presentation Dim FolderPath As String Dim pp As String ...

  2. Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)

    用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...

  3. 『科学计算』科学绘图库matplotlib学习之绘制动画

    基础 1.matplotlib绘图函数接收两个等长list,第一个作为集合x坐标,第二个作为集合y坐标 2.基本函数: animation.FuncAnimation(fig, update_poin ...

  4. python-day64--web框架

    http协议. 一.HTTP简介 1.HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输 ...

  5. python-day7--%s与%d的使用,python2中的input及raw_input

    #coding:utf-8 #utf-8格式打开#%s %d# name='egon'# age=18# print('my name is',name)# print('my name is my ...

  6. node搭建本地服务器

    随着前端不断发展,node基本已经成为必备,在调试的时候经常需要服务器,在之前的做法通常是去下载一个phpstudy 或者 xampp等启动一个服务,作为一个前端人虽然可以借助各种工具,但是怎么能不懂 ...

  7. sql server server 2005任务导入导出功能选项没有的解决方法

         出现这个问题主要原因是安装的sql server是Express版本的,或者已经安装了Express版本之后安装了企业版的.但是SQL图形管理工具仍然是SQL Server Manageme ...

  8. Awk 从入门到放弃(2)– 分隔符 学习笔记

    转:http://www.zsythink.net/archives/1336 学习输入分隔符FS及输出分隔符OFS 通过-v 修改内置的变量,在$1 $2 之间不指定 ‘,’, 会做合并输出.

  9. kernel build command

    Uboot: make ARCH=arm CROSS_COMPILE=${CC} distclean make ARCH=arm CROSS_COMPILE=${CC} am335x_evm_defc ...

  10. maven install deploy

    1.安装到本地仓库 install jar to local fs mvn .jar -DgroupId=com.bonc -DartifactId=licenseVerify-.jar -Dvers ...