sso*_*nez 9 momentjs typescript webpack angular
我正在尝试在 TypeScript 中使用“moment-duration-formation”,但是即使它有效,webpack 仍然抱怨它无法在线找到“format”方法:
return moment.duration(value, "minutes").format("...");
Run Code Online (Sandbox Code Playgroud)
这是package.json
{
"devDependencies": {
"@types/moment-duration-format": "^1.3.7"
},
"dependencies": {
"moment": "^2.18.1",
"moment-duration-format": "^1.3.0"
}
}
Run Code Online (Sandbox Code Playgroud)
和 tsconfig.json:
{
"compilerOptions": {
"typeRoots": [ "./node_modules/@types" ]
}
}
Run Code Online (Sandbox Code Playgroud)
在 (angular2) 组件中:
import * as moment from "moment";
import * as momentDurationFormat from "moment-duration-format";
...
let result: string = moment.duration(this.selectedHangarDistance * 10, "minutes").format("...")
Run Code Online (Sandbox Code Playgroud)
我也尝试过
import "moment-duration-format";
Run Code Online (Sandbox Code Playgroud)
但它并没有改变任何东西:
[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);
Run Code Online (Sandbox Code Playgroud)
这* as moment很重要,因为import moment from "moment"在通过时会导致类型问题momentDurationFormatSetup。
| 归档时间: |
|
| 查看次数: |
9281 次 |
| 最近记录: |