![](/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.