0
我正在使用Visual Studio代碼。打印機接口屬性未顯示在自動清單列表中
我有一個接口'車輛'有4個可選屬性,並在類'福特'中實現。
當我在class中單擊時,我看不到接口的屬性。然後在設置接口中的屬性值時發生錯誤併發生錯誤。
請參考下面的截圖。
我正在使用Visual Studio代碼。打印機接口屬性未顯示在自動清單列表中
我有一個接口'車輛'有4個可選屬性,並在類'福特'中實現。
當我在class中單擊時,我看不到接口的屬性。然後在設置接口中的屬性值時發生錯誤併發生錯誤。
請參考下面的截圖。
,因爲你沒有在你的類聲明場name
你得到一個錯誤。只要您在班級中使用正確的名稱聲明該字段,錯誤消息將消失:
interface Vehicle{
name?: string;
// rest of the code
}
class Ford implements Vehicle{
name:string; // <- as soon as you add the field the error message will go away
constructor(){
this.name="vimal";
}
}
爲什麼我要實現字段? 「福特」類中的'name:string'是一個聲明。 'this.name =「vimal」'是執行或分配 如果我錯了,請糾正我。 – Vimalraj
@VimalRaj您仍然需要在類聲明中聲明該字段。在構造函數中只有'this.name =「value」'不會自動聲明域 – dotnetom