我尝试在 TypeScript 中使用“moment-duration-formation”,但是,即使它有效,webpack 仍然抱怨它无法在线找到“format”方法:
return moment.duration(value, "minutes").format("...");
这是package.json
{
"devDependencies": {
"@types/moment-duration-format": "^1.3.7"
},
"dependencies": {
"moment": "^2.18.1",
"moment-duration-format": "^1.3.0"
}
}
还有 tsconfig.json:
{
"compilerOptions": {
"typeRoots": [ "./node_modules/@types" ]
}
}
在(Angular2)组件中:
import * as moment from "moment";
import * as momentDurationFormat from "moment-duration-format";
...
let result: string = moment.duration(this.selectedHangarDistance * 10, "minutes").format("...")
我也尝试过
import "moment-duration-format";
但这并没有改变任何事情:
[at-loader] 中出现错误 ./src/app/components/building/shop/shop.component.ts:190:81 TS2339: “持续时间”类型上不存在属性“格式”。
对我有用的解决方案:
import * as moment from "moment";
import momentDurationFormatSetup from "moment-duration-format";
momentDurationFormatSetup(moment);
* as moment
很重要,因为 import moment from "moment"
在通过 momentDurationFormatSetup
时会导致类型问题。
运行
yarn add --dev @types/moment-duration-format
或 npm install --save-dev @types/moment-duration-format