是否可以将样式表的 url 动态注入到组件中?
就像是:
styleUrls: [
'stylesheet1.css',
this.additionalUrls
]
或者(一厢情愿并注意这只是假代码):
export class MyComponent implements dynamicUrls {
ngDynamicUrls() {
this.inject(['anotherStylesheet.css', 'anotherStylesheet2.css']);
}
}
因为如果你要能够覆盖某些样式stylesheet1
如果无法访问它,您该怎么做?我唯一的想法就是要有活力styleUrls
不知何故,但从我所能找到的情况来看,我认为这是不可能的。
有任何想法吗?
它可能以某种可能对我有用的黑客方式。您可以操作 Angular 2 组件装饰器,创建您自己的装饰器并使用您的属性返回 Angular 的装饰器。
import { Component } from '@angular/core';
export interface IComponent {
selector: string;
template?: string;
templateUrl?: string;
styles?: string[];
styleUrls?: string[];
directives?: any;
providers?: any;
encapsulation?: number;
}
export function CustomComponent( properties: IComponent): Function {
let aditionalStyles: string;
try {
aditionalStyles = require(`path to aditional styles/${ properties.selector }/style/${ properties.selector }.${ GAME }.scss`);
properties.styles.push(aditionalStyles);
} catch (err) {
console.warn(err)
}
}
return Component( properties );
}
并在您的组件中将默认的 Angular 2 @Component 替换为新的。
import { CustomComponent } from 'path to CustomComponent';
@CustomComponent({
selector: 'home',
templateUrl: './template/home.template.html',
styleUrls: [ './style/home.style.scss']
})
export class ......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)