相关疑难解决方法(0)

如何在Vue模板中显示异步数据

在显示异步加载的Vue模板数据的情况下,我很有趣。在我的特定情况下,我需要显示产品对象的标题属性:

<td class="deals__cell deals__cell_title">{{ getProduct(deal.metal).title }}</td>
Run Code Online (Sandbox Code Playgroud)

但是该产品当前未加载,因此标题根本不会呈现。我找到了一个可行的解决方案:如果未加载产品,则在解决诺言之后,请调用getProduct函数:

getProduct (id) {
  if (!this.rolledMetal.all.length) {
    this.getRolledMetal()
      .then(() => {
        this.getProduct(id)
      })
    return {
      title: ''
    }
  } else {
      return this.getRolledMetalById(id)
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,也许您知道更优雅的解决方案,因为我认为这有点复杂:)

javascript templates asynchronous vue.js

5
推荐指数
2
解决办法
9669
查看次数

标签 统计

asynchronous ×1

javascript ×1

templates ×1

vue.js ×1