【问题标题】:Access vue components created via v-for from an array of objects从对象数组中访问通过 v-for 创建的 vue 组件
【发布时间】:2018-12-31 00:47:53
【问题描述】:

我有以下模板,我想在v-for语句中调用动态创建的组件的方法。

例如,我想在每一行上调用row.getSubtotal() 方法。我不知道该怎么做,因为this.rows 返回的是原始数组而不是组件数组。

 <template>
    <div>
        <table class="table table-bordered">
            <thead>
                <th  v-for="c in columns" v-bind:class="[c.className ? c.className : '']" :key="c.code">{{c.label}}</th>
            </thead>
            <tbody>
            <row v-for="(row, index) in rows"
                 :index="index+1"
                 :init-data="row"
                 :columns="columns"
                 :key="row.hash"
                 :hash="row.hash"
                 v-on:remove="removeRow(index)"></row>
            </tbody>
        </table>
        <div class="d-flex">
            <table>
                <tr>
                    <td>Unique SKUs:</td>
                    <td>{{rows.length}}</td>
                    <td>Total units:</td>
                    <td>{{totalUnits}}</td>
                </tr>
            </table>
            <span class="flex-fill"></span>
            <button class="btn" @click="newRow">Nueva línea</button>
        </div>
    </div>
</template>

&lt;row&gt; 元素是一个 Vue 组件,它是通过 rows 属性创建的,其中包含具有每个 rows 属性的对象数组。例如:

...
import Row from './Row'

export default {
    name: "OrderTable",
    components: {Row},
    data: () => ({
        hashes: [],
        rows: [
           {hash: '_yug7', sku: '85945', name: 'Coconut butter', price: 20},
           {hash: '_g484h', sku: '85745', name: 'Coconut oil', price: 15},
           {hash: '_yug7', sku: '85945', name: 'Cramberry juice', price: 5},
        ],
        fixedColumns: [
            {code: 'index', label: '#'},
            {code: 'sku', label: 'SKU'},
            {code: 'name', label: 'Product name', className: 'text-left align-middle'},
            {code: 'quantity', label: 'Units'},
            {code: 'price', label: 'Price', className: 'text-right align-middle'}
        ]
    }),
    computed: {
        totalUnits: function () {
            for(let x in this.rows) {
                // HERE I WANT TO CALL A METHOD IN THE ROW COMPONENT
                // For example this.rows[x].getSubtotal()
            }
        }
    },
...

【问题讨论】:

    标签: vue.js vuejs2 vue-component v-for


    【解决方案1】:

    在每个组件上动态创建ref属性,然后调用:

    <template>
        <div>
            <table class="table table-bordered">
                <thead>
                    <th  v-for="c in columns" v-bind:class="[c.className ? c.className : '']" :key="c.code">{{c.label}}</th>
                </thead>
                <tbody>
    
                <!-- Add the ref attribute to each row -->
                <row v-for="(row, index) in rows"
                     :ref="`myRow${index}`"
                     :index="index+1"
                     :init-data="row"
                     :columns="columns"
                     :key="row.hash"
                     :hash="row.hash"
                     v-on:remove="removeRow(index)"></row>
    
                </tbody>
            </table>
            <div class="d-flex">
                <table>
                    <tr>
                        <td>Unique SKUs:</td>
                        <td>{{rows.length}}</td>
                        <td>Total units:</td>
                        <td>{{totalUnits}}</td>
                    </tr>
                </table>
                <span class="flex-fill"></span>
                <button class="btn" @click="newRow">Nueva línea</button>
            </div>
        </div>
    </template>
    

    要调用组件上的方法,请执行以下操作:

    computed: {
            totalUnits: function () {
                for(let (x, index) in this.rows) {
                    let row = this.$refs[`myRow${index}`]
                    // You now have an instance of the component
                    let subtotal = row.getSubtotal()
                }
            }
        },
    

    $refs 属性的更多信息在这里:what's the real purpose of 'ref' attribute?

    【讨论】:

      【解决方案2】:

      考虑将v-forv-model 组合以从行子级导出您需要的所有数据。你的&lt;row /&gt; 需要实现 v-model 接口,所以look it up

      当然,您可以使用操作 $emit(),$on(), etc. 并让行在每次需要时向父组件发出其状态信号。

      希望这会为您指明正确的方向。

      【讨论】:

      • 感谢您的回答,但不,这不能解决我的问题。即使我听到孩子们发出的事件,我也会单独获得该项目的数据(数量、折扣、价格......),而不是表/购物车中的所有项目。我需要对所有数量和所有小计进行求和
      • 每个孩子都会发出自己的状态,是的。但是父母必须将它们存储在某种集合(数组)中。然后,您将能够使用 Array.map()、Array.reduce() 等处理此集合以获取总数。
      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 2016-08-14
      • 2019-02-27
      • 2021-05-24
      • 1970-01-01
      • 2021-11-18
      • 2022-01-03
      • 2020-10-09
      相关资源
      最近更新 更多