如何使chrome devtools识别moment.js

use*_*261 7 google-chrome google-chrome-devtools momentjs

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

然后在代码中使用了它:

import moment from 'moment'
...
let x = moment()
debugger
Run Code Online (Sandbox Code Playgroud)

在控制台上,我得到一个有趣的问题:

> 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)
Run Code Online (Sandbox Code Playgroud)

为什么我无法在控制台中处理片刻?

小智 35

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

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

然后你可以在那里正常使用它


小智 8

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

在控制台中,您可以编写类似

moment("20111031", "YYYYMMDD").fromNow(); 
Run Code Online (Sandbox Code Playgroud)