【问题标题】:MomentJS warning in Vue app after importing it from webpack从 webpack 导入后,Vue 应用程序中的 MomentJS 警告
【发布时间】:2018-12-21 23:58:07
【问题描述】:

搜索了一会儿后,我看到所有有关此警告的帖子都是在使用 MomentJS 时引起的,但就我而言,此警告是在将其导入我的 main.js 文件后显示的,甚至没有开始使用它第一次,我什至还没有在我的代码的任何部分添加“new Date()”或任何时刻调用。

这是我收到的全部警告:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: function Vue (options) {
  if ("development" !== 'production' &&
    !(this instanceof Vue)
  ) {
    warn('Vue is a constructor and should be called with the `new` keyword');
  }
  this._init(options);
}, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.eval [as createFromInputFallback] (webpack-internal:///./node_modules/moment/moment.js:320:98)
    at configFromInput (webpack-internal:///./node_modules/moment/moment.js:2606:19)
    at prepareConfig (webpack-internal:///./node_modules/moment/moment.js:2577:13)
    at createFromConfig (webpack-internal:///./node_modules/moment/moment.js:2544:44)
    at createLocalOrUTC (webpack-internal:///./node_modules/moment/moment.js:2631:16)
    at createLocal (webpack-internal:///./node_modules/moment/moment.js:2635:16)
    at hooks (webpack-internal:///./node_modules/moment/moment.js:12:29)
    at Function.Vue.use (webpack-internal:///./node_modules/vue/dist/vue.esm.js:4850:14)
    at eval (webpack-internal:///./src/main.js:51:46)
    at Object../src/main.js (http://localhost:8080/app.js:14037:1)

我在项目中添加 momentJS 的方式是运行:

npm i moment -D

然后在 main.js 我有以下内容:

import * as Moment from 'moment'

Vue.use(Moment)

window.app = new Vue({
    el: '#app',
    router: Router,
    store: store,
    render: h => h(App)
})

我知道这是一个警告,但在每次页面刷新时都有它有点烦人。最好避免它。

有什么线索吗?提前致谢。

【问题讨论】:

  • Vue.use(Moment) 确实执行代码。你甚至可以在你的堆栈跟踪中看到:at Function.Vue.use

标签: javascript vue.js webpack momentjs


【解决方案1】:

您可能不想将moment 传递给Vue.use()use() 会将其视为插件(如文档中的 here 所述)并尝试在将 Vue 作为参数传递时调用它,当然 moment 不会低估。这很可能是您收到该错误的原因。如果你想要一个 Vue 时刻插件,有 one here。如果您只想要moment,则没有理由将其传递给use

【讨论】:

  • 哦,我明白了……如果我想让它全球化,你有什么建议?喜欢 window.moment = require('moment')?
  • 嗯,我真的很喜欢“vue-moment”的想法,我已经在使用“vue-awesome”,它是 vue 的一个很棒的字体插件。谢谢!
  • Vue 文档有一个 example 将 axios 添加到 Vue.prototype 以获得通用访问权限。不确定这在其他方面的应用范围有多广。
  • 没有问题!我最终安装了 vue-moment! :) 我不需要太复杂的时间,所以如果至少有基本的东西,我很好!我会尽可能将其作为答案进行检查。再次感谢!
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 2018-05-30
  • 2018-03-31
  • 2017-02-25
  • 2021-12-12
  • 2018-05-07
  • 2018-10-17
  • 1970-01-01
相关资源
最近更新 更多