[英]correct ES6 import for adding moment precise range plugin
I'm using typescript within an angular 4 project and want to use moment-precise-range-plugin.我在 angular 4 项目中使用 typescript,并想使用 moment-precise-range-plugin。
I've installed moment
following https://momentjs.com/docs/#/use-it/typescript/ but i want to add the preciserange plugin http://momentjs.com/docs/#/plugins/preciserange/ .我已经在https://momentjs.com/docs/#/use-it/typescript/之后安装了moment
但我想添加 preciserange 插件http://momentjs.com/docs/#/plugins/preciserange/ 。
Install for Node.js for the preciserange plugin is at https://codebox.org.uk/pages/moment-date-range-plugin and specifies require('moment-precise-range-plugin');
精确范围插件的 Node.js 安装位于https://codebox.org.uk/pages/moment-date-range-plugin并指定require('moment-precise-range-plugin');
but there is no Typescript install.但是没有 Typescript 安装。
I've tried import { Moment } from 'moment-precise-range-plugin';
我试过import { Moment } from 'moment-precise-range-plugin';
but get the following typescript error但得到以下打字稿错误
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';'
Any ideas very much welcome!非常欢迎任何想法!
I'm using the moment-precise-range-plugin in an Angular 4 project and was able to import it with:我在 Angular 4 项目中使用 moment-precise-range-plugin 并且能够导入它:
import 'moment-precise-range-plugin';
I imported it after importing moment: import * as moment from 'moment';
我在导入时刻后导入它: import * as moment from 'moment';
One thing to note: my TS linter still warns me that preciseDiff()
will cause problems, but it all runs just fine.需要注意的一件事:我的 TS linter 仍然警告我preciseDiff()
会导致问题,但它运行得很好。 Try that out and see if it works.尝试一下,看看它是否有效。
NOTE: I'm using Angular 10.注意:我使用的是 Angular 10。
It works with me when import it as -> import 'moment-precise-range-plugin';
将它导入为 -> import 'moment-precise-range-plugin';
and add these 3 lines:并添加以下 3 行:
declare module 'moment' {
function preciseDiff(start: string | Date | moment.Moment, end: string | Date | moment.Moment, convertToObject?: boolean): any;
}
I fixed it by adding a definition file in src/typings.d.ts我通过在 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.