首页 > 解决方案 > nuxt.js 中 asyncData 中属性的奇怪强制

问题描述

我正在尝试在 Nuxt.js 中使用 asyncData,但在我看来,并非每个属性都可以按原样放置在这里。例如 Moment (moment.js) 和 DateTime (luxon) 的实例,它们被序列化为字符串:

import { DateTime } from 'luxon'
const moment = require('moment')
...
asyncData(context) {
    return {
      date1: moment(),
      date2: DateTime.local(),
      pureDate: new Date()
    }
  },
mounted() {
  console.log(typeof this.date1) // string ("2019-06-11T16:24:00.746Z")
  console.log(typeof this.date2) // string ("2019-06-13T19:24:00.748+03:00")
  console.log(typeof this.pureDate) // object (Thu Jun 13 2019 19:24:00 GMT+0300 (Moscow Standard Time))
}

其他一些复杂对象属性引发警告:

Cannot stringify arbitrary non-POJOs OpenPositions

有人请向我解释这种行为

沙盒片段

github上的demo repo

标签: javascriptvue.jsmomentjsnuxt.jsluxon

解决方案


推荐阅读