Ractive.js模板循环

Ractive.js template looping

本文关键字:循环 js Ractive      更新时间:2023-09-26

我正在进行ajax调用,并下拉页面总数:

$.get(path, function( data ) {
    ractive.set({
    'articles' : data.articles,
    'totalpages' : data.totalpages
    });
});

有什么方法可以从总页数中呈现分页按钮吗?类似(假设总页数=4):

{{#if loop totalpages times:num}}
  <a href="#">{{num}}</a> | 
{{/if}}

将输出

<a href="#">1</a> | <a href="#">2</a> | <a href="#">3</a> | <a href="#">4</a>

我看过Mustache的文档,但Mustache不太一样。

谢谢,Rob

在组件或实际实例中使用计算属性:

computed: {
    total: 'new Array(${totalPages})'               
}

然后使用:index(或任何您想要的)对each:上的索引进行别名

{{#each total:index}}
<a href="#">{{index+1}}</a>
{{/each}}

编辑:以上total计算属性是的Racive简写

computed: {
    total: function(){
        return new Array(this.get('totalPages'));
    }   
}