【发布时间】:2020-03-01 20:11:57
【问题描述】:
您知道在从 API 加载包含的所有数据后如何获取行数问题数组吗?我需要为分页表设置 totalRows 但
this.questions = this.questions.lenght
不工作并返回 null。
export default {
data() {
return {
questions: [],
fields: [
{ key: 'id', label: 'ID', sortable: true },
{ key: 'title', label: 'Title', sortable: true },
{ key: 'updated_at', label: 'Last update', sortable: true},
],
totalRows: 13,
currentPage: 1,
perPage: 5,
pageOptions: [5, 10, 15]
}
},
components: {},
created() {
axios.get('/api/question')
.then(res => this.questions = res.data.data)
.catch(error => console.log(error.response.data))
},
}
我还添加了返回我的 API 的内容
questions:Array[10]
--0:Object
-----body:"Sed minima nemo fuga libero. Rerum incidunt odio voluptatem aut quidem consequuntur. Odio deserunt labore voluptatem quo aut atque nemo."
-----id:1
-----title:Object
-----updated_at:"1 tydzień temu"
-----user:"Ahmad Mills"
【问题讨论】:
-
this.questions.lenght你打错了,应该是length。此外,您可能想查看计算属性。 vuejs.org/v2/guide/computed.html -
@Cristy 我尝试使用文档 Boostrap Vue 执行此操作,然后添加:mounted() { this.totalRows = this.questions.length },但仍然无法正常工作,您知道原因吗?
-
要么在需要的地方使用
this.questions.length,要么使用上面提到的计算属性。如果你在挂载时设置了长度,那么它将被设置为 0,因为 API 请求还没有发送。 -
@StanisławSzewczyk 你确定:
.then(res => this.questions = res.data.data)吗?res.data.data实际上是问题数组所在的位置吗?