我是Vue.js的新手,試圖理解計算值。特別是,我從REST API返回json數據,並且該數據是一個名爲date的字段,位於unixtime(即1486231256
)中。我想將該字段轉換爲人類可讀,但該應用程序返回invalid date
。這裏是我的代碼:Vue.js 2 axios計算的值返回「無效日期」
<script>
import axios from 'axios'
import moment from 'moment'
export default {
data:() => ({
errors: [],
posts: []
}),
// Fetches posts when the component is created.
created() {
axios.get('https://example.com/api/v1/?uid=1', {
})
.then(response => {
this.posts = response.data
})
.catch(e => {
this.errors.push(e)
})
},
computed: {
unixform() {
return moment(this.posts.date, 'X').format('YYYY-MM-DD')
}
}
}
</script>
我不認爲此刻是這裏的問題,因爲如果我改變函數的返回簡單this.posts.date
或this.date
,我得到的undefined
錯誤。所以不知何故,我沒有訪問該屬性,我不明白爲什麼。
什麼是您迴應樣子?它是一個數組還是對象?你已經聲明'posts'是一個數組。 – abhishekkannojia
帖子返回一個包含59個對象的數組。在每個對象中都有很多字段,包括'date',但是如果我將帖子定義爲對象,則會得到完全相同的錯誤{} – Stephen
然後,'this.posts.date'是錯誤的。 'posts'是一個數組,屬性'date'在數組上不存在。你需要迭代數組'posts'來獲取單個對象,並在每個日期值上調用moment函數。 – abhishekkannojia