简体   繁体   English

如何在非反应模块中使用 moment() 库(对于 React 项目)?

[英]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.

相关问题 如何在未反应的项目中使用`eslint-config-airbnb`? - How can I use `eslint-config-airbnb` in a non-react project? 如何在非反应组件中连接到商店 - How can I connect to the store in non-react components 如何在rails上的非反应javascript中使用jquery? - How to use jquery in non-react javascript on rails? 如果没有非 React DOM 父级,则无法卸载 React 门户 - 如何在删除父级之前手动卸载门户? - React portal cannot be unmounted without non-react DOM parent - how can I unmount the portal manually before removing the parent? 是否可以将反应组件导出为未反应项目中的功能 - Is it possible to export react component as function in non-react project 我可以通过Javascript在非反应环境中包含单个React组件吗? - Can I include a single React component in a non-React environment via Javascript? 如何为React和non-react消费者提供解决方案? - How to provide solutions to both React and non-React consumers? 如何将反应网站链接到非反应 href? - How to link a react website to non-react href? 如何在非反应第三方 html 中渲染多个反应组件 - How do I render multiple react components in non-react 3rd party html 如何在React中控制非React组件(BokehJS)? - How to control a non-React component (BokehJS) in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM