gpt4 book ai didi

javascript - 如何使用 Vue.js 对 ajax 重复列表应用过滤器?

转载 作者:行者123 更新时间:2023-12-03 06:21:53 31 4
gpt4 key购买 nike

我试图在使用 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

有没有办法让过滤器在尝试过滤数据之前“等待”列表加载?

最佳答案

您能做的最快的事情就是使用方法而不是过滤器:

createdDate(game) {
if (game !== undefined && game.date !== undefined) {
return someFunctionThatWillFormatYourDate(game.date)
} else {
return ''
}
}

关于javascript - 如何使用 Vue.js 对 ajax 重复列表应用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38830306/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com