Css 角度2外部样式不';不要内联到标题
我在typescript中有此组件定义:Css 角度2外部样式不';不要内联到标题,css,angular,angular2-template,Css,Angular,Angular2 Template,我在typescript中有此组件定义: import {Component, ViewEncapsulation} from 'angular2/core'; @Component({ selector: 'my-app', templateUrl: '/views/sandbox.html', styleUrls: ['/styles/sandbox.css'], styles: [`.wow { background-color: red; }`],
import {Component, ViewEncapsulation} from 'angular2/core';
@Component({
selector: 'my-app',
templateUrl: '/views/sandbox.html',
styleUrls: ['/styles/sandbox.css'],
styles: [`.wow { background-color: red; }`],
encapsulation: ViewEncapsulation.Emulated
})
export class SandBox { }
根据这篇文章:
样式部分和外部样式表中的样式都应该通过角度内联到标题中
不幸的是,第二个没有被注入,angular只注入styles部分中的一个
我试过从浏览器访问/styles/sandbox.css,没问题。Angular还可以访问/views/sandbox.html,所以我不知道为什么它不会发生
我正在使用:“angular2”:“2.0.0-beta.2”(最新的beta AFAIK)我从
沙盒中做了一些测试,奇怪的是样式。css
仅适用于使用样式URL
属性中的相对路径:
@Component({
selector: 'my-app',
templateUrl: '/views/sandbox.html',
styleUrls: ['../styles/sandbox.css'],
styles: [`.wow { background-color: red; }`],
encapsulation: ViewEncapsulation.Emulated
})
export class AppComponent {
constructor() {
}
}
编辑
查看源代码后,Angular2禁止对
样式URL使用绝对路径。请看这一行:
-
导出函数isStyleUrlResolvable(url:string):布尔值{
如果(isBlank(url)| | url.length==0 | | url[0]='/')返回false;//修复此问题的最佳方法是为组件使用另一个css文件,并将其添加到样式url列表中。该文件更干净,并将随着组件的增长而扩展。您可以使用systemjs和commonjs模块依赖项加载程序来加载模板、样式和其他文件
在commonjs moduleId中:module.id
在systemJs中
import {Component} from '@angular/core';
@Component({
moduleId:module.id,
selector: "exf-header",
templateUrl: "header.html",
styleUrls:['header.css']
})
export class HeaderComponent {
}
当你删除styleUrls属性时,它会被注入吗?“styles”总是被注入的。而另一个不是。你不能在绝对URL中删除第一个斜杠,使它看起来像是对我有效的styles/sandbox.css
。它有效,谢谢!但是,我想知道这是否是一个bug…因为绝对URL与“templateUrl”一起工作t不使用“styleUrls”在查看源代码后,有一个测试阻止使用此…我更新了我的答案。我在angular repo中添加了一个关于此的问题:。我期待从他们那里得到答案;-)
import {Component} from '@angular/core';
@Component({
moduleId:module.id,
selector: "exf-header",
templateUrl: "header.html",
styleUrls:['header.css']
})
export class HeaderComponent {
}