[英]How can I use moment() library inside a non-react module (for a React project)?
I'm not sure how to import moment() into my React project.我不确定如何将 moment() 导入我的 React 项目。 I did
yarn add moment
but then can't access the library?我做了
yarn add moment
,但后来无法访问图书馆?
I used moment quite frequently.我经常使用时刻。
yarn add moment
should do. yarn add moment
应该做的。
Next import moment from "moment"
import moment from "moment"
Then you can call moment()
anywhere in the file for example:然后您可以在文件中的任何位置调用
moment()
例如:
moment().endOf("year").format("DD/MM/YYYY")
Regardless, if it is a new project, consider avoiding moment and choosing a different library无论如何,如果它是一个新项目,请考虑避免时刻并选择不同的库
import moment from 'moment';
...
const Footer = () => {
return (
<footer className="main-footer">
<strong>
Copyright © {moment().format('Y')}
</strong>
All rights reserved.
</footer>
);
};
...
export default Footer;
FYI, moment js
is deprecated.仅供参考,
moment js
已被弃用。 Consider using other library, like date-fns
考虑使用其他库,例如
date-fns
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.