angular - ไม่พบคุณสมบัติ 'format' ในประเภท 'Duration'
ฉันกำลังพยายามใช้ "moment-duration-formation" ใน TypeScript แต่ถึงแม้ว่ามันจะทำงานได้ 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: คุณสมบัติ 'format' ไม่มีอยู่ในประเภท 'Duration'
คำตอบ #1
วิธีแก้ปัญหาที่ใช้ได้ผลสำหรับฉัน:
import * as moment from "moment";
import momentDurationFormatSetup from "moment-duration-format";
momentDurationFormatSetup(moment);
การใช้ * as moment มีความสำคัญเนื่องจาก import moment from "moment" ก่อให้เกิดปัญหาประเภทเมื่อถูกส่งไปยัง momentDurationFormatSetup
คำตอบ #2
รัน yarn add --dev @types/moment-duration-format หรือ npm install --save-dev @types/moment-duration-format
