[英]correct ES6 import for adding moment precise range plugin
我在 angular 4 项目中使用 typescript,并想使用 moment-precise-range-plugin。
我已经在https://momentjs.com/docs/#/use-it/typescript/之后安装了moment
但我想添加 preciserange 插件http://momentjs.com/docs/#/plugins/preciserange/ 。
精确范围插件的 Node.js 安装位于https://codebox.org.uk/pages/moment-date-range-plugin并指定require('moment-precise-range-plugin');
但是没有 Typescript 安装。
我试过import { Moment } from 'moment-precise-range-plugin';
但得到以下打字稿错误
Could not find a declaration file for module 'moment-precise-range-plugin'. 'c:/Users/[UserName]/app/node_modules/moment-precise-range-plugin/moment-precise-range.js' implicitly has an 'any' type. Try npm install @types/moment-precise-range-plugin if it exists or add a new declaration (.d.ts) file containing declare module 'moment-precise-range-plugin';'
非常欢迎任何想法!
我在 Angular 4 项目中使用 moment-precise-range-plugin 并且能够导入它:
import 'moment-precise-range-plugin';
我在导入时刻后导入它: import * as moment from 'moment';
需要注意的一件事:我的 TS linter 仍然警告我preciseDiff()
会导致问题,但它运行得很好。 尝试一下,看看它是否有效。
注意:我使用的是 Angular 10。
将它导入为 -> import 'moment-precise-range-plugin';
并添加以下 3 行:
declare module 'moment' {
function preciseDiff(start: string | Date | moment.Moment, end: string | Date | moment.Moment, convertToObject?: boolean): any;
}
我通过在 src/typings.d.ts 中添加一个定义文件来修复它
import * as moment from 'moment';
export = moment;
declare module 'moment' {
interface Moment {
preciseDiff(start: string | Date | moment.Moment): string;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.