javascript - Riot.js 如何重复标记一定次数?

标签 javascript riot.js

我只需要重复标签 5 次。我阅读了文档,但没有意识到如何做这样的事情:

<some-tag>
   <star repeat={5} />
</some-tag>

我只发现了这种丑陋的方式:

<some-tag>
   <star each={stars} />

   this.stars = new Array(5);
</some-tag>

最佳答案

查看源代码后,this您可能会感兴趣。

如果您计划使用 each属性,你必须定义某种数组让 riot 传递给 forEach .

jsfiddle是另一个想法(不是最优的),但我认为它说明了这一点。另一个解决方案是使用 Riot 的 mixin系统构建某种 repeat属性:

https://jsfiddle.net/aunn3gt0/2/

在您尝试使用 <yield/> 之前,这是一个很好的解决方案标签。那么它可能需要进行调整。这是它的工作原理:

var repeat = {
  init: function() {
    var repeats = [],
        count = this.opts.repeat

    this.on('mount', function() {
      if (!count) return

      var impl = this.root.innerHTML
      this.root.removeAttribute('repeat')

      while(repeats.length < count) {
        var clone = this.root.cloneNode()
        this.root.parentNode.insertBefore(clone, this.root)
        clone.innerHTML = impl
        repeats.push(riot.mount(clone))
      }
    })

    this.on('unmount',function(){
      repeats.forEach(function(tag,i){
        tag.unmount()
      })
    })

  }
}

然后用this.mixin(repeat)附加它并按照您的预期使用它:

<tag repeat="10">Repeated 10 times.</tag>

关于javascript - Riot.js 如何重复标记一定次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34384830/

相关文章:

javascript - 在加载多个 div 时应用动画

javascript - 如何在 laravel 项目中挂载 riot 标签

JavaScript:sessionStorage 丢失项目

javascript - 更新下拉列表中的值

javascript - 均匀间隔的响应式瓷砖网格 - 纯 css 有可能吗?

javascript - 在 Sapui5 中处理日期

javascript - 提高事件密集型防暴应用程序的代码质量/组织

javascript - DOM 不会根据附加 JQuery 的子标签进行更新

javascript - 在不使用多个 "then' 的情况下链接 Promise"

javascript - 如何从输入类型访问文件名和其他属性被隐藏