场景
我知道从 API 调用返回的数据的形状,并且我希望它通过 TypeScript 类具有一些扩展功能。我已经知道我可以执行以下操作来满足此要求:
this.http
.get(url)
.map((res: Response) => {
return res.json().map((obj: Type) => {
return Object.assign(new Type(), obj);
});
})
// And so on
Run Code Online (Sandbox Code Playgroud)
是否可以进行类似于以下内容的通用实现:
get<T>(url: string): Observable<T> {
return this.http
.get(url)
.map((res: Response) => {
return res.json().map((obj: T) => {
return Object.assign(new T(), obj); // not sure how to approach this
});
});
}
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于返回一个直接的 JavaScript 对象,但我不确定如何在泛型类型上运行分配。
是否可以在三元条件下执行多个操作?类似于以下内容(不起作用):
condition ? () => {
// Perform multiple actions within this delegate function if the condition is true
} : // Perform an action if the condition is false;
Run Code Online (Sandbox Code Playgroud) 我有以下调色板,具有各种色调值,应用于我的material-theme.scss文件中的多个主题:
$green: mat-palette($mat-green, A400);
$blue: mat-palette($mat-light-blue, A400);
$red: mat-palette($mat-red);
$red-warn: mat-palette($mat-red, A100);
Run Code Online (Sandbox Code Playgroud)
在我的material-styles.scss文件中,我有一个用于根据当前主题定义样式的 mixin:
@mixin style-theme($theme) {
$p: map-get($theme, primary);
$a: map-get($theme, accent);
$w: map-get($theme, warn);
$primary: mat-color($p);
$accent: mat-color($a);
$warn: mat-color($w);
$primary-contrast: mat-contrast($p, 500);
$accent-contrast: mat-contrast($a, 500);
$warn-contrast: mat-contrast($w, 500);
// Apply styling based on values above
}
Run Code Online (Sandbox Code Playgroud)
主题创建如下:
.light-green {
$default-theme: mat-light-theme($green, $blue);
@include style-theme($default-theme);
@include angular-material-theme($default-theme);
}
Run Code Online (Sandbox Code Playgroud)
我有可能获得当前应用调色板的对比度吗?就像现在一样,我只能硬编码函数的$hue值mat-contrast。