Angular 2 外部样式未内联到 header
Angular 2 external style doesn't get inlined to header
我在打字稿中有这个组件定义:
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 { }
根据这篇文章:http://blog.thoughtram.io/angular/2015/06/25/styling-angular-2-components.html
样式部分和外部样式表中的样式都应通过 angular.
内联到 header 中
不幸的是,第二个没有被注入,angular只注入样式部分中的那个。
我试过从浏览器访问/styles/sandbox.css,没问题。 Angular 也可以访问 /views/sandbox。html 所以我不知道为什么它没有发生。
我正在使用:"angular2":“2.0.0-beta.2”(最新 beta AFAIK)
我做了一些测试,奇怪的是 sandbox.css
的样式仅适用于在 styleUrls
属性中使用相对路径的情况:
@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 阻止 styleUrls
使用绝对路径。看到这一行:
-
export function isStyleUrlResolvable(url: string): boolean {
if (isBlank(url) || url.length === 0 || url[0] == '/') return false; // <-----
var schemeMatch = RegExpWrapper.firstMatch(_urlWithSchemaRe, url);
return isBlank(schemeMatch) || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
}
希望对你有帮助,
蒂埃里
解决此问题的最佳方法是为您的组件使用另一个 css 文件并将其添加到您的 StyleUrls 列表中。更清洁,并且会随着组件的增长而扩展。
您可以使用 systemjs 和 commonjs 模块依赖加载器,加载模板、样式和其他文件。
在 commonjs moduleId 中:module.id
在 systemJs __moduleName
import {Component} from '@angular/core';
@Component({
moduleId:module.id,
selector: "exf-header",
templateUrl: "header.html",
styleUrls:['header.css']
})
export class HeaderComponent {
}
我在打字稿中有这个组件定义:
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 { }
根据这篇文章:http://blog.thoughtram.io/angular/2015/06/25/styling-angular-2-components.html 样式部分和外部样式表中的样式都应通过 angular.
内联到 header 中不幸的是,第二个没有被注入,angular只注入样式部分中的那个。
我试过从浏览器访问/styles/sandbox.css,没问题。 Angular 也可以访问 /views/sandbox。html 所以我不知道为什么它没有发生。
我正在使用:"angular2":“2.0.0-beta.2”(最新 beta AFAIK)
我做了一些测试,奇怪的是 sandbox.css
的样式仅适用于在 styleUrls
属性中使用相对路径的情况:
@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 阻止 styleUrls
使用绝对路径。看到这一行:
-
export function isStyleUrlResolvable(url: string): boolean { if (isBlank(url) || url.length === 0 || url[0] == '/') return false; // <----- var schemeMatch = RegExpWrapper.firstMatch(_urlWithSchemaRe, url); return isBlank(schemeMatch) || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset'; }
希望对你有帮助, 蒂埃里
解决此问题的最佳方法是为您的组件使用另一个 css 文件并将其添加到您的 StyleUrls 列表中。更清洁,并且会随着组件的增长而扩展。
您可以使用 systemjs 和 commonjs 模块依赖加载器,加载模板、样式和其他文件。
在 commonjs moduleId 中:module.id 在 systemJs __moduleName
import {Component} from '@angular/core';
@Component({
moduleId:module.id,
selector: "exf-header",
templateUrl: "header.html",
styleUrls:['header.css']
})
export class HeaderComponent {
}