可以使用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. 5-15 devise(git指南,部分认为重要的内容的译文)

    git指南:https://github.com/plataformatec/devise 维基百科上有how to 的很多文章. Stackoverflow有大量问题的答案. 全的文档devise: ...

  2. zk请求和响应对

    zk的请求和响应是通过id对应上的: 请求头(RequestHeader)和响应头(ReplyHeader)共用一个xid,它的本质是ClientCnxn类中的一个计数器. 1. 首先看客户端: Pa ...

  3. 李阳音标速成MP3文本

    第一节:前元音 No. 1 [i:]穿针引线长衣音,简称"长衣音" 字母:e字母:ee 字母:ea字母:ie字母:ei 其发音要领是发音时舌尖抵下齿,前舌尽量抬高.舌位高于/i/: ...

  4. html <frame>标签使用

    标签定义 frameset 中的一个特定的窗口(框架) frameset中的每个框架都可以设置不同的属性,比如border,scrolling,noresize等 frame的常用属性 width: ...

  5. ehlib ado 删除选中记录 的方法

    procedure TForm1.Button1Click(Sender: TObject); var I: Integer; begin do begin DBGridEh1.DataSource. ...

  6. Flask初级(九)flash与前台交互get详解

    Project name :Flask_Plan templates:templates static:static @app.route('/') def hello_world(): return ...

  7. 判断当前应用程序处于前台还是后台 ANDROID

    /**     *判断当前应用程序处于前台还是后台     *      * @param context * @return         */    public static boolean ...

  8. hibernate缓存清除(转)

    文章有点杂,这不是原文,谢谢贡献者 http://www.360doc.com/content/16/0413/16/32415095_550307388.shtml 一.hibernate一级缓存( ...

  9. 安装win8时提不能在gpt磁盘中安装

    首先,你要知道,GPT和NTFS根本就是两码事儿.所谓的GPT,是指可扩展固件接口 (EFI) 使用的磁盘分区架构,是与主启动记录 (MBR) 分区架构相对应的,是一种磁盘分区架构.而ntfs,fat ...

  10. STL标准库-迭代器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 本节主要介绍STL六大部件中的Iterators迭代器. 在语言方面讲,容器是一个class template, 算法是一个仿函 ...