css – 动态style在角度2?

前端之家收集整理的这篇文章主要介绍了css – 动态style在角度2?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
可以将样式表的url动态注入到组件中吗?

就像是:

  1. styleUrls: [
  2. 'stylesheet1.css',this.additionalUrls
  3. ]

或(一厢情愿的想法,并注意到这只是假的代码):

  1. export class MyComponent implements dynamicUrls {
  2.  
  3. ngDynamicUrls() {
  4. this.inject(['anotherStylesheet.css','anotherStylesheet2.css']);
  5. }
  6. }

因为如果你能够从样式表1中覆盖某些样式而无需访问它,那么你应该怎么做呢?我唯一的想法是以某种方式拥有动态styleUrls,但我不认为这是甚么可能从我能找到.

有任何想法吗?

解决方法

有可能在某些方面对我来说有用.您可以操纵Angular 2 Component装饰器,创建自己的,并返回Angular的装饰器与您的属性.
  1. import { Component } from '@angular/core';
  2.  
  3. export interface IComponent {
  4. selector: string;
  5. template?: string;
  6. templateUrl?: string;
  7. styles?: string[];
  8. styleUrls?: string[];
  9. directives?: any;
  10. providers?: any;
  11. encapsulation?: number;
  12. }
  13.  
  14. export function CustomComponent( properties: IComponent): Function {
  15. let aditionalStyles: string;
  16.  
  17. try {
  18. aditionalStyles = require(`path to aditional styles/${ properties.selector }/style/${ properties.selector }.${ GAME }.scss`);
  19. properties.styles.push(aditionalStyles);
  20. } catch (err) {
  21. console.warn(err)
  22. }
  23. }
  24.  
  25. return Component( properties );
  26. }

在您的组件中,用新的组件替换默认角度2 @Component.

  1. import { CustomComponent } from 'path to CustomComponent';
  2.  
  3. @CustomComponent({
  4. selector: 'home',templateUrl: './template/home.template.html',styleUrls: [ './style/home.style.scss']
  5. })
  6. export class ......

猜你在找的CSS相关文章