我正在尝试在使用 AJAX 检索的列表上应用过滤器,我遇到的问题是 Vue.js 尝试在加载和显示列表之前应用过滤器。
这是我到目前为止所拥有的:
<template>
<div class="grid__container games">
<h2>Games</h2>
<div v-if="games">
<table>
<tr>
<th>Player One</th>
<th>Results</th>
<th>Player Two</th>
<th>Played At</th>
</tr>
<tr v-for="game in games.data">
<td>{{ game.player_one }}</td>
<td>{{ game.player_one_goals }} - {{ game.player_two_goals }}</td>
<td>{{ game.player_two }}</td>
<td>{{ [ game.created_at, "YYYY-MM-DD h:mm:ss" ] | moment "dddd, MMMMM, Do YYYYY" }}</td>
</tr>
</table>
</div>
</div>
</template>
<script>
import auth from '../auth'
export default {
data() {
return {
data: '',
games: ''
}
},
methods: {
getGames() {
this.$http
.get('/api/games', { headers: auth.getAuthHeader() }).then((data) => {
this.data = data
this.games = JSON.parse(this.data.body)
}, (error) => {
console.log(error)
})
}
},
created: function () {
this.getGames()
}
}
</script>
在这里,我尝试将过滤器应用到在日期创建的游戏上,它在尝试加载页面时结束抛出错误
Uncaught ReferenceError: string is not defined
在尝试过滤数据之前,有没有办法让过滤器“等待”加载列表?