“持续时间”类型上不存在属性“格式”

问题描述 投票:0回答:2

我尝试在 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: “持续时间”类型上不存在属性“格式”。

angular typescript webpack momentjs
2个回答
3
投票

对我有用的解决方案:

import * as moment from "moment";
import momentDurationFormatSetup from "moment-duration-format";
momentDurationFormatSetup(moment);

* as moment
很重要,因为
import moment from "moment"
在通过
momentDurationFormatSetup
时会导致类型问题。


0
投票

运行

yarn add --dev @types/moment-duration-format
npm install --save-dev @types/moment-duration-format

© www.soinside.com 2019 - 2024. All rights reserved.