【问题标题】:Can I use HyperX with Snabbdom我可以将 HyperX 与 Snabbdom 一起使用吗
【发布时间】:2017-02-20 19:35:34
【问题描述】:

HyperX 是一个模块,它将标记的模板文字转换为 hyperscript 函数,就像 virtual-dom 中包含的函数一样。

Snabbdom 使用类似超脚本的函数来构建它的 vdom,但它的第二个参数不同。它的属性被各种“模块”使用,而不是属性;

h('div', {
  props: {title: someString}, // snabbdom/modules/props
  classes: {selected: isSelected}, // snabbdom/modules/class
  on: {click: doSomething}, // snabbdom/modules/eventlisteners
  style: {color: someColor} // snabbdom/modules/style
}, ['children']);

是否可以像这样使用hyperxsnabbdom 的超标函数:

const h = require('snabbdom/h');
const hyperx = require('hyperx');
const hx = hyperx(h);

let vdom = hx`
  <div 
    title=${someString} 
    class-selected={isSelected} 
    on-click={doSomething} 
    style={({color: someColor})}
  >
   children
  </div>
`;

【问题讨论】:

    标签: virtual-dom hyperscript


    【解决方案1】:

    是的,你可以!

    var snabbdom = require('snabbdom')
    var patch = snabbdom.init([ // Init patch function with chosen modules
      require('snabbdom/modules/class'), // makes it easy to toggle classes
      require('snabbdom/modules/props'), // for setting properties on DOM elements
      require('snabbdom/modules/style') // handles styling on elements with support for animations  
    ])
    var h = require('snabbdom/h')
    var hyperx = require('hyperx')
    var hx = hyperx(h)
    
    var title = 'world'
    var wow = [1,2,3]
    var tree = hx`<div>
      <h1 y="ab${1+2}cd">hello ${title}!</h1>
      ${hx`<i>cool</i>`}
      wow
      ${wow.map(function (w, i) {
        return hx`<b>${w}</b>\n`
      })}
    </div>` 
    patch(document.body, tree)
    

    检查工作代码here

    【讨论】:

    • 您的示例中的属性不起作用。

      没有“y”属性。孩子似乎可以工作,但您不能附加事件监听器、类、样式或其他属性。

    • 我明白了。似乎是patch函数无法渲染用hyperx生成的vnode对象,至少不是所有的东西。我尝试包括所有模块,特别是事件监听器和属性,但这没有区别。我还检查了tree 对象,它具有属性,这就是为什么我得出结论认为补丁功能无法正常工作
    猜你喜欢
    • 2023-03-25
    • 2020-08-04
    • 2018-10-23
    • 2011-02-22
    • 2021-03-16
    • 2016-11-20
    • 2019-01-25
    • 2011-06-11
    • 2013-11-09
    相关资源
    最近更新 更多