问题
I get all data from firebase and store it on array and from that array i take all posted_at data (time when was something posted). My goal is to retrieve all that data in some sort of time format and problem is that it won't retrieve multiple values. I am using Vuejs.
<template>
<div v-for="data in array">
{{ time }}
</div>
</template>
<script>
import moment from 'moment'
export default{
data(){
return{
array:[]//example:1577200868199, 1577200868189,...
}
},
computed:{
time(){
moment(this.array.posted_at).format('MMMM Do YYYY, h:mm:ss a')
}
}
}
</script>
P.S. I have tried using a for and a while loop but it's not working
回答1:
This is a nice case to use Vue filter
<template>
<div v-for="data in array">
{{ data | formatTime }}
</div>
</template>
<script>
import moment from 'moment'
export default{
data(){
return{
array: []//example:1577200868199, 1577200868189,...
}
},
filters: {
formatTime: function (value) {
return moment(value).format('MMMM Do YYYY, h:mm:ss a')
}
}
}
</script>
回答2:
With moment you can only parse one item at a time, so you can change your code to loop over your array and parse each item individually. It would look something like this.
this.array.forReach(posted_at => {
moment(posted_at).format('MMMM Do YYYY, h:mm:ss a')
})
来源:https://stackoverflow.com/questions/59494301/how-can-you-retrieve-multiple-values-with-momentjs-from-firebase