【发布时间】:2021-05-14 17:30:24
【问题描述】:
我有从服务器获取的用户数据,这些数据按年龄降序排列。当用户数据传递给子组件时,它不会显示按 v-data-table 中年龄的 desc 顺序排序的数据,当我刷新页面时,它会显示按降序排序的数据。请帮我找出哪里出错了。我正在使用 vuetify 版本 2.3.10
在父组件中从服务器获取数据时如下:
[{full_name: 'James Ross', city: 'Toronto', age: 45 },{full_name: 'Stacey Purkis', city: 'Calgary', age: 32 }, {full_name: 'Jazz Alzo', city: 'London', age: 24 }]
子组件第一次加载用户数据如下:
[{full_name: 'Stacey Purkis', city: 'Calgary', age: 32 }, {full_name: 'Jazz Alzo', city: 'London', age: 24 }, {full_name: 'James Ross', city: 'Toronto', age: 45 }]
所以基本上它没有排序,当我刷新页面时,子组件中的用户数据显示完美完成
[{full_name: 'James Ross', city: 'Toronto', age: 45 },{full_name: 'Stacey Purkis', city: 'Calgary', age: 32 }, {full_name: 'Jazz Alzo', city: 'London', age: 24 }]
父组件
<template>
<div>
<Users :users="users"></Users>
</div>
</template>
<script>
import Users from 'views/lawyers/_users_table.vue';
export default {
components: {
Users
},
data: () => ({
users: [],
}),
created: function() {
this.fetchUsers();
},
methods: {
fetchUsers() {
this.$axios.get('/users.json')
.then(response => {
this.users = response.data;
})
}
}
};
</script>
子组件
<template>
<div>
<v-data-table
:headers="headers"
:items="users"
:disable-pagination="true"
v-model="users"
hide-default-footer
class="elevation-1"
>
<template slot="item" slot-scope="props">
<tr>
<td class="full-name">{{ props.item.full_name }}</td>
<td class="summary">{{ props.item.address}}</td>
<td class="experience">{{ props.item.age }} years</td>
</tr>
</template>
</v-data-table>
</div>
</template>
<script>
export default {
props: ['users'],
data: () => ({
headers: [
{ text: 'Name', value: 'full_name', sortable: false },
{ text: 'Address', value: 'address', sortable: false },
{ text: 'Age',value: 'age' }
]
}),
};
</script>
【问题讨论】:
标签: javascript vue.js sorting vuetify.js