![](/img/trans.png)
[英]How can I check if the value of a parameter passed to a javascript function is null?
[英]Typescript: How can I set an optional parameter if no value is passed in a derived class?
我有一個基類:
export class ClientBase {
constructor(private uri: string, private httpClient: HttpClient) { }
// My Various Methods
}
我有幾個從ClientBase派生的類
例如:
@Injectable()
export class GizmoService extends ClientBase {
constructor(private http: HttpClient)
{
super(environment.gizmoUri, http);
}
};
因此,在GizmoService中,我想在一種特殊情況下專門傳遞Uri。
因此,理想情況下,我想執行以下操作:
@Injectable()
export class GizmoService extends ClientBase {
constructor(private http: HttpClient, uri?: string)
{
if (!uri) uri = environment.gizmoUri;
super(uri, http);
}
};
但是我收到錯誤消息,超級必須是構造函數中的第一個調用。
我如何解決這個問題而不必對現有代碼庫進行大量更改? (即,不更改每個調用以傳遞2個參數)
您始終可以將uri
參數uri
默認值。
@Injectable()
export class GizmoService extends ClientBase {
constructor(private http: HttpClient, uri: string = environment.gizmoUri) {
super(uri, http);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.