Javascript 如何使用 npm 在 ES6 中导入 moment.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36476402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import moment.js in ES6 using npm?
提问by Santhosh Aineri
I am using angular js and nodejs along with ES6. I want to import the moment.js in the angular js code. I did 'npm install moment --save'
我正在使用 angular js 和 nodejs 以及 ES6。我想在angular js代码中导入moment.js。我做了'npm install moment --save'
Now I am able to see moment.js file in moment folder which is inside node modules.
现在我可以在节点模块内的 moment 文件夹中看到 moment.js 文件。
and in my app.js file I have wriiten like this
在我的 app.js 文件中,我是这样写的
'import moment from 'moment';
But if search something with date range It is showing error in console. Can anybody help me how to do this..?
但是,如果搜索具有日期范围的内容,它会在控制台中显示错误。任何人都可以帮助我如何做到这一点..?
回答by cmd
Currently, to use ES6 module syntax, you need to use a transpiler e.g. Babel, since node.js and most browsers do not yet support ES6 module syntax.
目前,要使用 ES6 模块语法,您需要使用转译器,例如 Babel,因为 node.js 和大多数浏览器尚不支持 ES6 模块语法。
Babel and webpack are great for this. Hereis a nice example:
Babel 和 webpack 非常适合这一点。这是一个很好的例子:
Once you have it configured properly, you invoke moment as such:
一旦你正确配置了它,你就可以这样调用 moment:
import moment from 'moment';
console.log(moment.now());
回答by flawyte
Since version 2.10.0moment is written in ES6 :
由于版本 2.10.0moment 是用 ES6 编写的:
import moment from './node_modules/moment/src/moment';
// Note the 'src/' to import the ES6 version and not the CJS build
However for now it seems there is no way to import only a subset of functions, you have to import the whole thing.
但是现在似乎没有办法只导入函数的一个子集,你必须导入整个东西。
回答by Alice Tolokova
The option, that worked for me is:
对我有用的选项是:
import * as moment from 'moment';
回答by AlainIb
Use this librairie instead : https://github.com/urish/angular-moment
改用这个图书馆:https: //github.com/urish/angular-moment
load the js files :
加载js文件:
<script src="components/moment/moment.js"></script>
<script src="components/angular-moment/angular-moment.js"></script>
inject moment dependency in your app controller :
在您的应用程序控制器中注入时刻依赖:
var myapp = angular.module('myapp', ['angularMoment']);
回答by zabusa
the code you written wont work.its not
你写的代码不起作用。它不是
//wont work
'import moment from moment'; this is wrong.
//this will work
import moment from 'moment';
or
import 'moment';
if these wont work.then try it using web-pack to expose moment to globally.
如果这些不起作用。然后尝试使用 web-pack 向全球公开时刻。
回答by atilkan
This is what you are looking.
这就是你正在寻找的。
import moment from 'moment'
import it from 'moment/locale/it'
import { utc } from 'moment'
moment.locale('it')
You can read the related issue here. https://github.com/moment/moment/issues/2608
您可以在此处阅读相关问题。 https://github.com/moment/moment/issues/2608
回答by Pedro Furtado
Here, this works:
在这里,这有效:
import moment from 'moment';
window.moment = moment;