![](/img/trans.png)
[英]Types of property 'min' are incompatible. Type 'null' is not assignable to type 'number | undefined'
[英]Typescript: “X” is not assignable to type, types of property “Y” are incompatible
注意:我對Angular2和Typescript還是很陌生,所以對我的無知感到抱歉。
問題:我要執行的操作是從結束日期/時間中減去開始日期/時間,然后在公式中使用該值進行計算以顯示為“ calc”。 問題是我可以給calc一個可以工作的靜態數,但是如果我嘗試使用數學公式,則在編譯時會給出各種錯誤:
1模擬訂單.ts(4,14):錯誤TS2322:類型'{order_no:string; 預定:字符串; 橫向:弦; start_time:字符串; 檢查:字符串; “ stop _...”不能分配給“ Order []”類型。
輸入'{order_no:string; 預定:字符串; 橫向:弦; start_time:字符串; 檢查:字符串; “ stop _...”不能分配給“訂單”類型。
屬性“檢查”的類型不兼容。
“字符串”類型不可分配給“數字”類型。
以下是我遇到這些問題的子組件。 參見第28行this.calc:
// order.ts
import { Component, OnInit } from '@angular/core';
export class Order {
order_no: string;
scheduled: string;
lateral: string;
start_time: string;
checks: number;
stop_time: string;
status: string;
approx_cfs: string;
approx_hrs: string;
approx_af: string;
calc: number;
constructor(data: {} = {}) {
this.order_no = data["order_no"] || "";
this.scheduled = data["scheduled"] || "";
this.lateral = data["lateral"] || "";
this.start_time = data["start_time"] || "";
this.checks = data["checks"] || "";
this.stop_time = data["stop_time"] || "";
this.status = data["status"] || "";
this.approx_cfs = data["approx_cfs"] || "";
this.approx_hrs = data["approx_hrs"] || "";
this.approx_af = data["approx_af"] || "";
this.calc = (!this.stop_time ? ((new Date().getTime() - new Date(this.start_time).getTime()) / 1000.0 / 60.0 / 60.0) * this.checks * 0.0825 : ((new Date(this.stop_time).getTime() - new Date(this.start_time).getTime()) /1000.0 / 60.0 / 60.0) * this.checks * 0.0825);
console.log(this.calc);
};
};
我敢肯定,部分問題是我試圖對日期使用數學運算,然后將結果分配給數字類型。
以下調用此組件以在服務中使用:
// order.service.ts
import { Injectable } from '@angular/core';
import { Order } from './order';
import { ORDERS } from './mock-orders';
@Injectable()
export class OrderService {
getOrders(): Promise<Order[]> {
return Promise.resolve(ORDERS);
}
}
以下文件是每次服務運行時提取的數據數組,以描述將從數據庫接收的數據。
// mock-orders.ts
import { Order } from './order'
export const ORDERS: Order[] = [
{order_no: '12345',
scheduled: '08/16/16 13:45',
lateral: 'L1-8-1-T7, L1-8-1-T6',
start_time: '08/16/16 15:45',
checks: '23.25',
stop_time: '08/17/16 15:30',
status: 'Delivered',
approx_cfs: '25.00',
approx_hrs: '22',
approx_af: '45.38',
},
{order_no: '12346',
scheduled: '08/17/16 11:45',
lateral: 'L1-8-1-T7, L1-8-1-T6',
start_time: '08/17/16 15:30',
checks: '20.25',
stop_time: '',
status: 'Running',
approx_cfs: '25.00',
approx_hrs: '10',
approx_af: '20.63',
},
{order_no: '12346',
scheduled: '08/17/16 11:45',
lateral: 'L1-8-1-T7, L1-8-1-T6',
start_time: '08/17/16 15:30',
checks: '20.25',
stop_time: '',
status: 'Running',
approx_cfs: '25.00',
approx_hrs: '10',
approx_af: '20.63',
},
{order_no: '12346',
scheduled: '08/17/16 11:45',
lateral: 'L1-8-1-T7, L1-8-1-T6',
start_time: '08/17/16 15:30',
checks: '20.25',
stop_time: '',
status: 'Running',
approx_cfs: '25.00',
approx_hrs: '10',
approx_af: '20.63',
}
];
因為Date.parse()
首先嘗試將輸入轉換為數字,然后從中獲取等效日期,並且在js中使用非數字字符(例如'-'或'/')轉換字符串將導致NaN結果為日期。解析還將返回NaN。 在這種情況下,可以使用new Date(str: String)
從有效的日期字符串中創建日期對象。 您還可以使用getTime()
函數對日期進行數學運算。
let data = { order_no: '12346', scheduled: '08/17/16 11:45', lateral: 'L1-8-1-T7, L1-8-1-T6', start_time: '08/17/16 15:30', checks: '20.25', stop_time: '', status: 'Running', approx_cfs: '25.00', approx_hrs: '10', approx_af: '20.63', }; this.order_no = data["order_no"] || ""; this.scheduled = data["scheduled"] || ""; this.lateral = data["lateral"] || ""; this.start_time = data["start_time"] || ""; this.checks = data["checks"] || ""; this.stop_time = data["stop_time"] || ""; this.status = data["status"] || ""; this.approx_cfs = data["approx_cfs"] || ""; this.approx_hrs = data["approx_hrs"] || ""; this.approx_af = data["approx_af"] || ""; this.calc = (!this.stop_time ? ((new Date().getTime() - new Date(this.start_time).getTime()) / 1000.0 / 60.0 / 60.0) * this.checks * 0.0825 : ((new Date(this.stop_time).getTime() - new Date(this.start_time).getTime()) /1000.0 / 60.0 / 60.0) * this.checks * 0.0825); console.log(calc);
問題是您正試圖將字符串類型值分配給類型為數字的檢查變量。
start_time: string; checks: number; stop_time: string;
分配的值:-
start_time: '08/17/16 15:30', checks: '20.25', stop_time: '',
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.