Translate {{if @last}} handlebars templates to vue

2019-09-14 22:11发布

I want to progressively switch from Backbone + Handlebars to Vue, but I am having some troubles with Handlebars templates.

In one of my templates I have something like this:

{{#each tHeads}}
  {{#if @last}}
    {{#each th}}
      {{#if iWidth}}
        <col style="width: {{iWidth}}px;"/>
      {{/if}}
    {{/each}}
  {{/if}}
{{/each}}

I want to translate this to a vue template, but cannot find the corresponding for @last helper from Handlebars.

标签: vue.js vuejs2
1条回答
三岁会撩人
2楼-- · 2019-09-14 22:55

There is no implementation for something like @last in Vue, as Evan You explained here that it will be costly to have such feature which are also used not that frequently for each v-for loop.

You can define a method isLast for this and use it like following:

<div v-if="isLast(index)">

JS

methods: {
  isLast (index) {      
    return this.list.length === index + 1
  }
}
查看更多
登录 后发表回答