【问题标题】:Access $moment inside async fetch of NuxtJS在 NuxtJS 的异步获取中访问 $moment
【发布时间】:2020-09-07 12:54:19
【问题描述】:

我使用的是 NuxtJS 版本 2.12.2。我试图在异步获取中访问 $moment,但它返回 app.$moment 不是函数。下面是我的sn-p:

pages/_key.vue

<template>
  <div></div>
</template>

<script>
import { mdSign } from '@/constants/encryption.js'

var pickerValue = new Date()
var dTime = pickerValue.getTime()

export default {
  async fetch({ store, params, app }) {
    const theUuid = app.$generateUUID()  // this is a global property
    const theSignature = mdSign({
      uuid: theUuid
    })
    const body = {
      cpKey: params.key,
      day: app.$moment(pickerValue).format('YYYY-MM-DD'),
      sign: theSignature,
      time: dTime
    }
    await store.dispatch('example/fetchHistory', body)
  }
}
</script>

<style lang="scss" scoped></style>

plugins/filter.js

import Vue from 'vue'
import { translations } from '@/constants/pinyin.js'

const moment = require('moment')
Vue.use(require('vue-moment'), {
  moment
})

nuxt.config.js

export default {
  ...
  ...
  plugins: [
    '~/plugins/filters.js',
    ...
    ...
  ]
  ...
  ...

【问题讨论】:

  • 在我看来,名称不匹配。当您在 nuxt 配置中写入 plugins/filters.js 时,您的文件是 plugins/filter.js。这也可能是一个复制错误,但我想不出你的代码还有什么问题。
  • 你试过了吗?
  • @DeanChristianArmada 使用安装方法将其安装为新插件,您可以从任何地方轻松访问它

标签: vue.js nuxt.js


【解决方案1】:

将@nuxtjs/moment 添加到您的项目中:

npm i @nuxtjs/moment

将@nuxtjs/moment 添加到您的nuxt.config.js 模块中:

modules: [
'@nuxtjs/moment'
]

对于您通常在 Vue 应用程序中使用的许多库,都有 Nuxtjs 特定的包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 2019-09-08
    • 2021-06-16
    • 2021-10-23
    • 2023-03-26
    相关资源
    最近更新 更多