与Vuejs的Moment.js

Set*_*Lar 103 momentjs vue.js

我尝试使用如下所示打印日期时间 vue-for

{{ moment().format('MMMM Do YYYY, h:mm:ss a') }}
Run Code Online (Sandbox Code Playgroud)

但是,它没有出现.这只是一片空白.我如何尝试在vue中使用时刻?

Pan*_*lis 188

使用您的代码,vue.js尝试moment()从其范围访问该方法.

因此你应该使用这样的方法:

methods: {
  moment: function () {
    return moment();
  }
},
Run Code Online (Sandbox Code Playgroud)

如果你想传递一个日期moment.js,我建议使用过滤器:

filters: {
  moment: function (date) {
    return moment(date).format('MMMM Do YYYY, h:mm:ss a');
  }
}

<span>{{ date | moment }}</span>
Run Code Online (Sandbox Code Playgroud)

[试玩]

  • 如果使用es6,请不要忘记 - 从'时刻'导入时刻; (5认同)
  • 过滤器在 Vue 2+ 中被禁用。您应该改用计算属性。请参阅我对这个问题的回答。 (4认同)
  • Vue 3 中已弃用过滤器。我已经使用这种方法有一段时间了,现在正致力于替换相当多的代码,为更新到 Vue 3 做准备。https://v3.vuejs.org/guide/migration /filters.html#概述 (4认同)
  • 如何每秒更新视图中的时间 (2认同)

Gen*_*wen 116

如果您的项目是单页面应用程序(例如,由...创建的项目vue init webpack myproject),我发现这种方式最直观,最简单:

main.js中

import moment from 'moment'

Vue.prototype.moment = moment
Run Code Online (Sandbox Code Playgroud)

然后在您的模板中,只需使用

<span>{{moment(date).format('YYYY-MM-DD')}}</span>
Run Code Online (Sandbox Code Playgroud)

  • 我真的很喜欢这种使用 Moment 的方法。感谢分享!我刚刚实现了它,但按照文档中的建议进行了一些小更改,Vue.prototype.$moment = moment。https://vuejs.org/v2/cookbook/adding-instance-properties.html。 (3认同)
  • 对于我认为的任何项目,简单而完美,谢谢@Geng (2认同)

Paw*_*cki 24

在你package.json"dependencies"部分添加时刻:

"dependencies": {
  "moment": "^2.15.2",
  ...
}
Run Code Online (Sandbox Code Playgroud)

在您想要使用的组件中,导入它:

<script>
import moment from 'moment'
...
Run Code Online (Sandbox Code Playgroud)

并在同一组件中添加计算属性:

computed: {
  timestamp: function () {
    return moment(this.<model>.attributes['created-at']).format('YYYY-MM-DD [at] hh:mm')
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在这个组件的模板中:

<p>{{ timestamp }}</p>
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,如果您不想使用无参数函数,而是想使用如下函数: `date2day: function (date) {return moment(date).format('dddd')}` 你不能使用 `computed `,并且应该使用`methods`代替。 (2认同)

max*_*max 11

我在单个文件组件中使用了Vue 2.0.

npm install moment 在安装了vue的文件夹中

<template>
  <div v-for="meta in order.meta">
    {{ getHumanDate(meta.value.date) }}
  </div>
</template>
<script>
    import moment from 'moment';
    export default {
         methods: {
            getHumanDate : function (date) {
                return moment(date, 'YYYY-MM-DD').format('DD/MM/YYYY');
            }
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

  • 为什么是方法,为什么不是计算? (2认同)

小智 8

这是一个使用名为vue-moment.

除了将 Moment 实例绑定到 Vue 的根范围之外,该库还包含momentduration过滤器。

这个例子包括本地化并且使用 ES6 模块导入,这是一个官方标准,而不是 NodeJS 的 CommonJS 模块系统要求。

import Vue from 'vue';

import moment from 'moment';
import VueMoment from 'vue-moment';

// Load Locales ('en' comes loaded by default)
require('moment/locale/es');

// Choose Locale
moment.locale('es');

Vue.use(VueMoment, { moment });
Run Code Online (Sandbox Code Playgroud)

现在你可以直接在你的 Vue 模板中使用 Moment 实例,而无需任何额外的标记:

<small>Copyright {{ $moment().year() }}</small>
Run Code Online (Sandbox Code Playgroud)

或过滤器:

<span>{{ 3600000 | duration('humanize') }}</span>
<!-- "an hour" -->
<span>{{ [2, 'years'] | duration('add', 1, 'year') | duration('humanize') }}</span>
<!-- "3 years" -->
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:这个答案因其长度和内容而被标记为低质量。您可能希望通过解释这如何回答 OP 的问题来改进它。 (2认同)

小智 8

// plugins/moment.js

import moment from 'moment';

moment.locale('ru');

export default function install (Vue) {
  Object.defineProperties(Vue.prototype, {
    $moment: {
      get () {
        return moment;
      }
    }
  })
}

// main.js

import moment from './plugins/moment.js';
Vue.use(moment);

// use this.$moment in your components
Run Code Online (Sandbox Code Playgroud)


Jir*_*son 6

对于 Vue 3 的 moment.js

npm install moment --save
Run Code Online (Sandbox Code Playgroud)

然后在任意组件中

import moment from 'moment'

...

export default {
  created: function () {
    this.moment = moment;
  },

...

<div class="comment-line">
   {{moment(new Date()).format('DD.MM.YYYY [&nbsp;] HH:mm')}}
</div>
Run Code Online (Sandbox Code Playgroud)


Muh*_*zad 6

Moment.js 与 Vue3 js

npm install moment --save   # npm
yarn add moment             # yarn
Run Code Online (Sandbox Code Playgroud)

主.ts

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

import moment from 'moment'

const app = createApp(App)

app.config.globalProperties.$moment = moment

app.use(router).mount('#app')
Run Code Online (Sandbox Code Playgroud)

vue3 js 组件中的使用时刻

{{ $moment(item.created_at).format("YYYY-MM-DD") }}  // 2021-07-03
Run Code Online (Sandbox Code Playgroud)