[英]How can I using momentJS globally in VueJS 2?
I use vue js version 2.6.11我使用 vue js 版本 2.6.11
I try to set up in main.js like this:我尝试像这样在 main.js 中设置:
import moment from 'moment'
moment.locale('nl');
Object.definePrototype(Vue.prototype, '$moment', { value: moment });
But there exist error:但存在错误:
Uncaught (in promise) TypeError: Object.definePrototype is not a function
I try another way like this:我尝试这样的另一种方式:
moment.locale('nl')
Vue.prototype.$moment = moment
But when I run the page, on the console exist error:但是当我运行页面时,在控制台上存在错误:
[Vue warn]: Error in render: "ReferenceError: moment is not defined"
If I put import moment from 'moment'
directly in component vue, it works.如果我将import moment from 'moment'
直接放在组件 vue 中,它可以工作。 But I want to put it globally但我想把它放在全球
How can I solve this problem?我怎么解决这个问题?
Note:笔记:
My component vue like this:我的组件vue是这样的:
<template>
<b-card no-body>
...
<p class="mb-0 text-muted">{{formatDate(data.date)}}</p>
...
</b-card>
</template>
<script>
import moment from 'moment'
export default {
props: ['data'],
methods: {
formatDate(date) {
return this.$moment(date).format('YYYY MM DD')
},
},
}
</script>
You can try the following.您可以尝试以下方法。 In main.js
在main.js
import Vue from "vue";
import moment from 'moment'
moment.locale('de');
Vue.prototype.moment = moment;
And in your component you can directly use moment as below在您的组件中,您可以直接使用 moment 如下
<template>
<b-card no-body>
...
<p class="mb-0 text-muted">{{formatDate(data.date)}}</p>
...
</b-card>
</template>
<script>
export default {
props: ['data'],
methods: {
formatDate(date) {
return this.moment(date).format('YYYY MM DD')
},
},
}
</script>
You have wrong syntax.你有错误的语法。 There's no Object.definePrototype
, the function you need is Object.defineProperty
or Object.defineProperties
There's no Object.definePrototype
, the function you need is Object.defineProperty
or Object.defineProperties
Change your code更改您的代码
Object.definePrototype(Vue.prototype, '$moment', { value: moment });
to至
Object.defineProperty(Vue.prototype, '$moment', { value: moment });
Both define methods work fine for me.两种定义方法对我来说都很好。
Object.defineProperty(Vue.prototype, '$moment', { value: moment });
// or
Vue.prototype.$moment = moment;
Since the less code, I can not point out accurately why you can not use $moment.由于代码较少,我无法准确指出为什么不能使用 $moment。
But, I guess you use this.$moment in data() (?)但是,我猜你在 data() 中使用 this.$moment (?)
Tips: You can't use this
in data(), since the component isn't mounted yet.提示:您不能在 data() 中使用this
,因为该组件尚未安装。 Try to use it in mounted
or methods
section.尝试在mounted
或methods
部分使用它。
Also, the easiest way to use this.$moment, just import the package vue-moment
here .此外,使用 this.$moment 的最简单方法,只需在此处导入 package vue-moment
。 This package already define $moment.这个 package 已经定义了 $moment。 All you need to do is import it and type Vue.use(require("vue-moment"));
您需要做的就是导入它并输入Vue.use(require("vue-moment"));
in your main.js.在你的 main.js 中。
This does not answer your question directly, but I feel like I need to share this: Moment has been deprecated.这并不能直接回答您的问题,但我觉得我需要分享一下:Moment 已被弃用。 Please look into other alternatives if needed: https://www.npmjs.com/package/moment如果需要,请查看其他替代方案: https://www.npmjs.com/package/moment
This is a good replacement: https://www.npmjs.com/package/dayjs这是一个很好的替代品: https://www.npmjs.com/package/dayjs
i think its better to use vue-moment see the link blow:我认为最好使用 vue-moment 查看链接:
https://www.npmjs.com/package/vue-moment https://www.npmjs.com/package/vue-moment
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.