gpt4 book ai didi

javascript - 访问 Vue JS 实例监视对象中的 $refs 数组

转载 作者:行者123 更新时间:2023-12-02 23:56:03 24 4
gpt4 key购买 nike

我正在构建一个 Vue JS SPA,并在 v-app 内部有一个 Vuetify 数据表。我试图在数据表内的 filteredItems 计算属性watch 对象 内设置一个变量,但我不确定如何访问 $refs 数组在实例内部。

我知道可以通过以下方式在实例中访问数据:

this.$refs['prospectsTable'].filteredItems

但是,相同的引用名称在监视对象中不起作用。我尝试过:

$refs['prospectsTable'].filteredItems: function(newItems) {
console.log(newItems);
}

&

this.$refs['prospectsTable'].filteredItems: function(newItems) {
console.log(newItems);
}

如何访问 watch 函数内的 $refs 数组?

var p = new Vue({
el: '#prospectsApp',
data: () => ({}),

watch: function() {
// How can i access the $refs array inside of the watch function?
$refs['prospectsTable'].filteredItems: function() {

}
}
});




<div id="prospectsApp">
<v-app id="inspire" v-cloak>
<v-data-table ref="prospectsTable" v-model="selected" :headers="headers" :items="prospects" :pagination.sync="pagination" select-all item-key="id" class="elevation-1" >
<template v-slot:headers="props">
<tr>
<th><v-checkbox :input-value="props.all" color="#c79121" :indeterminate="props.indeterminate" primary hide-details @click.stop="toggleAllSelected"></v-checkbox></th>
<th v-for="header in props.headers" :key="header.text"
:class="['column sortable', pagination.descending ? 'desc' : 'asc', header.value === pagination.sortBy ? 'active' : '']"
@click="changeSort(header.value)">
<v-icon small>arrow_upward</v-icon>
@{{ header.text }}
</th>
</tr>
</template>

<template v-slot:items="props">
<tr :active="props.selected" @click="props.selected = !props.selected">
<td class="text-center align-middle">
<v-checkbox :input-value="props.selected" primary hide-details color="#c79121"></v-checkbox>
</td>
<td class="text-center align-middle">@{{ props.item.id }}</td>
<td>
<div>@{{ props.item.name }}</div>
<div v-show="props.item.contacted" class="label label-success"><span class="fa fa-check-square-o"></span> Contacted!</div>
</td>
<td>@{{ props.item.foodcat.title }}</td>
<td>@{{ props.item.contact_fname }}<span v-if="props.item.contact_lname"> @{{ props.item.contact_lname }}</span><span v-if="props.item.contact_title">, @{{ props.item.contact_title }}</span></td>
<td>@{{ props.item.response_notes }}</td>
<td class="text-right align-middle">
<button class="btn btn-primary btn-sm" @click="updateContacted(props.item.id)" v-show="!props.item.contacted"><span class="fa fa-check-square-o"></span> Mark As Contacted</button>
<button class="btn btn-primary btn-sm" @click="editProspect(props.item.id)"><span class="fa fa-edit"></span> Edit Lead</button>
<button class="btn btn-danger btn-sm" @click="removeProspect(props.item.id)"><span class="fa fa-trash"></span> Delete Lead</button>
</td>
</tr>
</template>
<template slot="no-data">
<p class="text-xs-center">No Data</p>
</template>
</v-data-table>
</v-app>
</div>

最佳答案

最好的方法是在挂载上添加观察者。尝试以下代码。

mounted(){
this.$watch(
() => {
return this.$refs.prospectsTable.filteredItems
},
(val) => {
console.log(val)
alert('App $watch $refs.counter.i: ' + val)
}
)
}

Codepen - https://codesandbox.io/s/j7wjjypnxw

关于javascript - 访问 Vue JS 实例监视对象中的 $refs 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55387933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com