【问题标题】:How to make chrome devtools to recognise moment.js如何让 chrome devtools 识别 moment.js
【发布时间】:2018-06-27 09:14:52
【问题描述】:

我正在处理ionic2 项目。我刚刚开始使用moment.js,但我有一个奇怪的问题。 我已经通过 npm 安装了:npm install moment -S

然后我在我的代码中使用了它:

import moment from 'moment'
...
let x = moment()
debugger

在控制台上我遇到了这个有趣的问题:

> x
< Moment {_isAMomentObject: true, _isUTC: false, _pf: {…}, _locale: Locale, _d: Wed Jun 27 2018 12:06:23, …}
> y = moment()
< VM770:1 Uncaught ReferenceError: moment is not defined
    at eval (eval at Phase (phase.ts:13), <anonymous>:1:1)
    at new Phase (phase.ts:13)
    at new Stage (stage.ts:10)
    at new HomePage (home.ts:39)
    at createClass (core.es5.js:10795)
    at createDirectiveInstance (core.es5.js:10621)
    at createViewNodes (core.es5.js:11971)
    at createRootView (core.es5.js:11876)
    at callWithDebugContext (core.es5.js:13007)
    at Object.debugCreateRootView [as createRootView] (core.es5.js:12468)

为什么我不能在控制台中使用 moment?

【问题讨论】:

  • 由于 moment() 不再在此上下文中使用(“调试器”是最后一行),我猜 V8 在此上下文中对其进行了优化。
  • 我在调试器后添加了let y = moment()。同样的错误。
  • 如果您还没有,也许在终端中运行node 并在其中要求moment。我就是这样做的。
  • 你能发布这段代码的转译版本吗?也许它会导致一些答案,

标签: google-chrome google-chrome-devtools momentjs


【解决方案1】:

可以像这样从浏览器控制台获取它:

fetch('https://momentjs.com/downloads/moment.min.js')
    .then(response => response.text())
    .then(text => eval(text))

【讨论】:

  • 由于某种原因它不允许我:Refused to connect to 'https://momentjs.com/downloads/moment.min.js' because it violates the following Content Security Policy directive:
【解决方案2】:

如果库不是以常规形式导入的,您将无法访问它;如果您想在 Chrome 控制台中使用moment(),您可以尝试打开official site,然后转到控制台。

在控制台中你可以写类似

moment("20111031", "YYYYMMDD").fromNow(); 

【讨论】:

    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多