简体   繁体   English

在@component 样式中使用变量

[英]Use variables inside @component styles

I want to generate some dynamic CSS and use it inside the styles of @component.我想生成一些动态 CSS 并在 @component 的样式中使用它。 I don't know exactly how to pass that variable and wonder even if it is possible to do this.我不确切知道如何传递该变量,并且想知道是否可以这样做。 Here is the piece of code that uses the variable "stylesArray".这是使用变量“stylesArray”的一段代码。

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    styles: stylesArray,
    encapsulation: ViewEncapsulation.None,
})
export class AppComponent {
    stylesArray: string[] = [
        // Generate some dynamic css here
    ];
}

You can do it with the host decorator property:您可以使用主机装饰器属性来做到这一点:

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    encapsulation: ViewEncapsulation.None,
    host: {
      '[style]': 'stylesArray'
    }
})
export class AppComponent {
    stylesArray: string[] = [
        // Generate some dynamic css here
    ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angular2的@Component中使用样式的变量? 如何在样式中使用名为leftposition的变量? - how to use variables in the styles used in @Component in angular 2 ? How can I use variable named leftposition in the styles? 如何在角度2组件内使用角度1变量 - how to use angular 1 variables inside the angular 2 component 角度2,在@Component样式中使用变量 - Angular 2, use variable in @Component styles 使用变量来更新角度组件内部的CSS? - Use variables to update internal CSS inside an angular component? 如何覆盖材料 Angular 组件 styles 中的 Sass 变量? - How can I override Sass variables in Material Angular component styles? 在自定义组件内的NgbDatePicker,如何传播验证器和样式? - NgbDatePicker inside a custom component, how to propagate validators and styles? 在 shadow-root 中插入组件时不应用角度样式 - Angular styles not applying when insert component inside shadow-root Angular 组件特定样式以使用 tailwind @apply 函数 - Angular component specific styles to use tailwind @apply function 如何在 angular 组件的styles.scss 中使用媒体查询? - How to use media queries in angular component's styles.scss? 你可以在 Angular 组件中同时使用 style 和 styleUrls 吗? - Can you use both styles and styleUrls in an Angular component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM