来自另一篇文章的简单示例...
new Vue({
el: '#app',
data: {
filters: {
id: '',
issuedBy: '',
issuedTo: ''
},
items: [{id:1234,issuedBy:'Operator',issuedTo:'abcd-efgh'},{id:5678,issuedBy:'User',issuedTo:'ijkl-mnop'}]
},
computed: {
filtered () {
const filtered = this.items.filter(item => {
return Object.keys(this.filters).every(key =>
String(item[key]).includes(this.filters[key]))
})
return filtered.length > 0 ? filtered : [{
id: '',
issuedBy: '',
issuedTo: ''
}]
}
}
})
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/><link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/><script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script><script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script><script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
<div id="app">
<b-table striped show-empty :items="filtered">
<template slot="top-row" slot-scope="{ fields }">
<td v-for="field in fields" :key="field.key">
<input v-model="filters[field.key]" :placeholder="field.label">
</td>
</template>
</b-table>
</div>
现在我明白了这是如何工作的,但我还集成了 apollo 来进行 graphql 查询。我有 Apollo 填充项目..
所以我添加了 apollo 和一个已安装的(用于阻止)
new Vue({
el: '#app',
apollo: {
searchPersons: GET_PERSON
},
data: {
filters: {
name: '',
location: '',
relocate: ''
},
},
computed: {
filtered () {
const filtered = this.items.filter(item => {
return Object.keys(this.filters).every(key =>
String(item[key]).includes(this.filters[key]))
})
return filtered.length > 0 ? filtered : [{
name: '',
location: '',
relocate: ''
}]
}
},
mounted: function () {
this.$apollo.queries.searchPersons.refetch().then((results) => {
this.totalRows = results.data.searchPersons.length
this.items = results.data.searchPersons
})
},
})
如果您想知道,这是我的 GET_PERSON graphql
import { gql } from "apollo-boost";
export const GET_PERSON = gql`
query {
searchPersons(keyword: "", fromSource: false){
name
location
relocate
currentSalary
resumeBody
personemailSet {
email
}
personphoneSet {
phoneType
verified
number
}
personskillsSet {
term
score
weight
}
personresumeattachmentSet {
attachment
}
personworkplacepreferenceSet{
name
label
}
}
}
`;
所以发生的情况是,表尝试加载(这很好),但它尝试在返回数据之前过滤并获取数据,所以我留下了以下错误
vue.runtime.esm.js?2b0e:619 [Vue warn]: 渲染错误:“TypeError: 无法读取未定义的属性‘filter’”
老实说,我觉得安装可能不是正确的方法?
感谢任何帮助。
谢谢!
最佳答案
因此最初将其定义为空数组。
data: {
filters: {
name: '',
location: '',
relocate: ''
},
items : []
//---^-----
},
关于javascript - VueJS 在渲染数据之前等待 Apollo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668110/