【问题标题】:How can I export a plugin from dayjs() in Javascript?How can I export a plugin from dayjs() in Javascript?
【发布时间】:2022-12-27 20:04:25
【问题描述】:

I am using the pluginisToday()to extend the functionality ofdayjs()but I don't know how to export isToday() so I can use it in other files.

import isToday from "dayjs/plugin/isToday";

export dayjs.extend(isToday);

【问题讨论】:

  • export default dayjs;

标签: javascript reactjs typescript dayjs


【解决方案1】:

I was able to get it done by creating my own dayjs class:

import dayjs from 'dayjs';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import 'dayjs/locale/en';

const appDayjs = dayjs;

appDayjs.extend(isSameOrBefore);
appDayjs.extend(isSameOrAfter);

export { appDayjs }; // OR export default appDayjs

If you are using TypeScript and you want a Dayjs similar type, you can use:

// ...

const appDayjsInstance = appDayjs();
type AppDayjs = typeof appDayjsInstance;

export {
  appDayjs,
  AppDayjs,
}

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2022-12-26
    • 2022-11-20
    • 2022-12-01
    • 2022-12-01
    • 2022-12-01
    • 2022-12-27
    • 2022-12-02
    • 2022-12-02
    相关资源
    最近更新 更多