[英]Use variable color in component.ts Angular
我有一组颜色,我想传递给子组件。
colors = ['#1e88e5', '#e53935', '#757575'];
我可以像上面那样使用硬编码十六进制值来做到这一点,但是有没有一种方法可以像下面那样使用全局变量。
colors = [$primary, $secondary, $normal];
由于我想在组件上而不是在scss
上使用全局变量,我不确定如何使用它。
任何帮助表示赞赏。
谢谢
您可以创建类并传递给子组件
这是一个例子,
color.ts
export class Color {
$primary = '#ff0000';
$secondary = '#ffff00';
}
在app.component.ts
import { Color} from './color';
@Component({
selector: 'my-app',
template: `<hello [color]="color"></hello>`
})
export class AppComponent {
color = new Color();
}
并在hello.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'hello',
template: `<h1 [ngStyle]="{color: color.$primary}">Hello</h1>`,
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent {
@Input() color: any;
}
这是Stackblitz演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.