【问题标题】:Translate {{if @last}} handlebars templates to vue 2.0将 {{if @last}} 车把模板翻译成 vue 2.0
【发布时间】:2017-06-05 08:02:26
【问题描述】:

我想逐步从 Backbone + Handlebars 切换到 Vue,但在使用 Handlebars 模板时遇到了一些问题。

在我的一个模板中,我有这样的东西:

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

我想把它翻译成一个 vue 模板,但是在 Handlebars 中找不到对应的 @last 助手。

【问题讨论】:

标签: vue.js vuejs2


【解决方案1】:

在 Vue 中没有像 @last 这样的实现,正如 Evan You 解释的 here ,拥有这样的功能成本很高,而且每个 v-for 循环的使用频率也不高。

您可以为此定义一个方法isLast 并使用它,如下所示:

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

JS

methods: {
  isLast (index) {      
    return this.list.length === index + 1
  }
}

【讨论】:

  • 谢谢,我试试看!
  • @bboydflo 建议避免在模板中直接使用方法,因为它们会在每个更改检测周期中执行。相反,请尝试使用计算属性
猜你喜欢
  • 2022-08-09
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多