每个计算的 Vue 属性

Vue For Each Computed Property

这个问题可能有一个非常简单的修复方法,但我似乎无法让它正常工作。我正在使用 Vuetify 数据 table 循环显示我的所有属性,但是当我使用计算 属性 时它似乎无法正常工作。我正在尝试将一些道具(街道、城市、州、邮政编码)组合成一个计算地址道具。所以当我单独使用每个道具时,它工作得很好:

<td class="text-xs-center">{{ props.item.street }}</td>
<td class="text-xs-center">{{ props.item.city }}</td>
<td class="text-xs-center">{{ props.item.state }}</td>
<td class="text-xs-center">{{ props.item.zip }}</td>

但是,如果在我的 Vue 模板的脚本部分中,我创建了一个计算的 属性 "fullAddress",例如:

computed: {
  fullAddress () {
    return this.street & '\n' & this.city & ', ' & this.state & ' ' & this.zip
  }
}

然后在上面的模板中使用它,例如:

<td class="text-xs-center">{{ props.item.fullAddress() }}</td>

它根本不起作用。我也尝试了许多其他方法来写出来,但没有任何效果。我是 Vue 的新手以及它如何使用计算属性,所以我确信我只是不了解它是如何正常工作的。

编辑:我正在使用 Vuetify 数据 table 循环遍历我的数据。我正在使用他们的文档来显示 table。就像我说的,我是 Vue 的新手,所以我想弄清楚这一切。我相信 :items="items" 是所有道具的 v-bind(类似于 v-for 循环?)。这是一个更完整的示例,说明 Vuetify 对非常简单的数据有什么 table:

<template>
  <v-data-table
    :headers="headers"
    :items="items"
    hide-actions
    class="elevation-1"
  >
    <template slot="items" slot-scope="props">
      <td>{{ props.item.name }}</td>
      <td class="text-xs-right">{{ props.item.street }}</td>
      <td class="text-xs-right">{{ props.item.city }}</td>
      <td class="text-xs-right">{{ props.item.state }}</td>
      <td class="text-xs-right">{{ props.item.zip }}</td>
    </template>
  </v-data-table>
</template>

<script>
  export default {
    data () {
      return {
        headers: [
          {
            text: 'Name',
            sortable: false,
            value: 'name'
          },
          { text: 'Street', value: 'street' },
          { text: 'City', value: 'city' },
          { text: 'State', value: 'state' },
          { text: 'Zip Code', value: 'zip' }
        ],
        items: [
          {
            name: 'Braums',
            street: '159 W Elm St',
            city: 'St. Louis',
            state: 'MO',
            zip: 83607
          },
          {
            name: 'McDonalds',
            street: '237 N Cup Way',
            city: 'Dallas',
            state: 'TX',
            zip: 47621
          }
        ]
      }
    }
  }
</script>

感谢您扩展问题。

你不能做什么:你不能有一个计算数组,因为计算不与数据项相关联,它们与组件相关联。按照您建议的编写方式,计算的每个 item 必须有不同的 this。但它的 this 是组件。

您可以创建一个基于 items 的计算,returns items 中的每个值都增加了您为该项目计算的 fullAddress 值。注意注意不要修改你原来的item;我使用 Object.assign 创建一个新副本。

然后将计算后的数组传递给 v-table 而不是传递 items.

我只是将新变量插入到 street 的位置以表明它有效。

Vue.use(Vuetify);

new Vue({
  el: '#app',
  data() {
    return {
      headers: [{
          text: 'Name',
          sortable: false,
          value: 'name'
        },
        {
          text: 'Street',
          value: 'street'
        },
        {
          text: 'City',
          value: 'city'
        },
        {
          text: 'State',
          value: 'state'
        },
        {
          text: 'Zip Code',
          value: 'zip'
        }
      ],
      items: [{
          name: 'Braums',
          street: '159 W Elm St',
          city: 'St. Louis',
          state: 'MO',
          zip: 83607
        },
        {
          name: 'McDonalds',
          street: '237 N Cup Way',
          city: 'Dallas',
          state: 'TX',
          zip: 47621
        }
      ]
    }
  },
  computed: {
    itemsWithFullAddress() {
      // This creates a new empty object, copies the item into it,
      // then calculates `fullAddress` and copies that entry into it
      return this.items.map((obj) => Object.assign({}, obj, {
        fullAddress: `${obj.street}\n${obj.city}, ${obj.state} ${obj.zip}`
      }));
    }
  }
});
<link href="//unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet" />
<script src="//unpkg.com/vue@latest/dist/vue.js"></script>
<script src="//unpkg.com/vuetify/dist/vuetify.min.js"></script>
<div id="app">
  <v-data-table :headers="headers" :items="itemsWithFullAddress" hide-actions class="elevation-1">
    <template slot="items" slot-scope="props">
      <td>{{ props.item.name }}</td>
      <td class="text-xs-right">{{ props.item.fullAddress }}</td>
      <td class="text-xs-right">{{ props.item.city }}</td>
      <td class="text-xs-right">{{ props.item.state }}</td>
      <td class="text-xs-right">{{ props.item.zip }}</td>
    </template>
  </v-data-table>
</div>