今天,腾讯正式开源发布 Omix 1.0, 让开发者使用 JSX 或 hyperscript 建立用户界面。css
灵活的插件体系和丰富的插件生态git
class Hello extends Omi.Component { render() { return <div> Hello {this.data.name}!</div> } } Omi.tag('hello', Hello) class App extends Omi.Component { install() { this.name = 'Omi' } handleClick(e) { this.name = 'Omix' this.update() } style() { return `h3{ color:red; cursor: pointer; }` } render() { return <div> <hello name={this.name}></hello> <h3 onclick={this.handleClick.bind(this)}>Scoped css and event test! click me!</h3> </div> } } Omi.render(new App(), '#container')
const $ = Omi.tags class Hello extends Omi.Component { render() { return $.div( 'Hello' + this.data.name+'!') } } Omi.tag('hello-tag', Hello) class App extends Omi.Component { handleClick(e) { alert(e.target.innerHTML) } render() { return $.div([ $.HelloTag({name: 'Omi'}), $.h3({onclick: this.handleClick}, 'scoped css and event test! click me!') ]) } }
const $ = Omi.tags $.tagName(selector) $.tagName(attrs) $.tagName(children) $.tagName(attrs, children) $.tagName(selector, children) $.tagName(selector, attrs, children)
海外有大量的工程师以为的 hyperscript 比 JSX 要更加简洁和方便,可是咱们团队内部喜欢 JSX 和 hyperscript 一半一半。可是没有关系 Omix 同时支持两种方式。下面稍微对比一下二者的使用差别:github
// JSX <ul id="bestest-menu"> {items.map( item => <li class="item" {...attrs(item.id)}>{item.title}</li> )} </ul>
vsnpm
// hyperscript-helpers $.ul('#bestest-menu', items.map( item => $.li('.item', attrs(item.id), item.title)) );
// JSX <MyList>{items.map(item => <MyItem id={item.id} title={item.title} /> )}</MyList>
vs框架
// hyperscript-helpers $.MyList(items.map(item => $.MyItem(item.id, item.title) ))
<MyComponent someProp={{x: 1, y: 2}}/>
vsthis
$.MyComponent({x: 1, y: 2})
Omix 对插件体系进行了升级,使用方便比从前更加简便,这里拿 omi-finger 做为例子, omi-finger 是 Omi的AlloyFinger插件,让你轻松在Omi项目里支持各类触摸事件和手势:spa
npm install omi-finger
import Omi from 'omix'; import 'omi-finger'; class App extends Omi.Component { handleTap(evt){ this.refs.touchArea.innerHTML+='<br/>Tap'; } handleSwipe(evt){ this.refs.touchArea.innerHTML+='<br/>Swipe-'+ evt.direction; } render() { return <div> <div omi-finger ref="touchArea" tap="handleTap" swipe="handleSwipe" > Tap or Swipe Me! </div> </div> } } Omi.render(new App(),"#container");
是否是超级简便。还在等什么,用到就是赚到,赶忙开始阅读 中文文档 或者在 Omi REPL 把玩一下!插件
This content is released under the MIT License.双向绑定