如何从方法中计算 属性?
How can I get computed property from methods?
首先,我使用 this 模块在 Vue 中进行无限加载。
为了在每次加载时添加元素,我将 json 数据从我的 API 服务器放入 数据对象 ,然后再次将数组保存到变量中在我将数组分成大小为“4”的组之后。但问题是它使用无法从计算机 属性 获取任何变量的方法事件处理程序附加元素。我是 Vue 的新手,我找不到任何关于此的信息。这是代码!
export default {
name: 'main',
data: () => ({
items: [],
line: []
}),
async created () {
this.items = await fetch('/videos').then(res => res.json())
},
computed: {
columns: function() {
return chunk(this.items, 4)
}
},
methods: {
onInfinite() {
setTimeout(() => {
const temp = []
const len = this.columns.length
for (let i = len + 1; i <= len + 5; i++) {
temp.push(this.columns[i])
console.log(this.columns[i]) //It prints 'undefined'
}
this.list = this.list.concat(temp)
this.$refs.myRefName.$emit('$InfiniteLoading:loaded')
}, 700)
},
},
components: {
InfiniteLoading
}
}
const len = this.columns.length // length of `columns` array
下一行:
for (let i = len + 1; i <= len + 5; i++) {
...this.columns[i]
您正在尝试访问数组长度以上的索引,这些值显然是未定义的。
您可能应该像这样遍历 columns
:
for (let column of columns) {
for (let item of column) { // asuming column is Array(4)
console.log(item)
如果您的 columns
是一个 'chunk' 数组,每个数组有 4 个元素,它应该可以工作。
你用这个关键字来得到这样的计算方法:
item.push(this.column[i]);
首先,我使用 this 模块在 Vue 中进行无限加载。
为了在每次加载时添加元素,我将 json 数据从我的 API 服务器放入 数据对象 ,然后再次将数组保存到变量中在我将数组分成大小为“4”的组之后。但问题是它使用无法从计算机 属性 获取任何变量的方法事件处理程序附加元素。我是 Vue 的新手,我找不到任何关于此的信息。这是代码!
export default {
name: 'main',
data: () => ({
items: [],
line: []
}),
async created () {
this.items = await fetch('/videos').then(res => res.json())
},
computed: {
columns: function() {
return chunk(this.items, 4)
}
},
methods: {
onInfinite() {
setTimeout(() => {
const temp = []
const len = this.columns.length
for (let i = len + 1; i <= len + 5; i++) {
temp.push(this.columns[i])
console.log(this.columns[i]) //It prints 'undefined'
}
this.list = this.list.concat(temp)
this.$refs.myRefName.$emit('$InfiniteLoading:loaded')
}, 700)
},
},
components: {
InfiniteLoading
}
}
const len = this.columns.length // length of `columns` array
下一行:
for (let i = len + 1; i <= len + 5; i++) {
...this.columns[i]
您正在尝试访问数组长度以上的索引,这些值显然是未定义的。
您可能应该像这样遍历 columns
:
for (let column of columns) {
for (let item of column) { // asuming column is Array(4)
console.log(item)
如果您的 columns
是一个 'chunk' 数组,每个数组有 4 个元素,它应该可以工作。
你用这个关键字来得到这样的计算方法:
item.push(this.column[i]);