将类添加到Angular2组件HTML标记不会应用样式
在Angular2中,当我在自定义Angular2组件上添加类标记时,如下所示:将类添加到Angular2组件HTML标记不会应用样式,html,css,angular,Html,Css,Angular,在Angular2中,当我在自定义Angular2组件上添加类标记时,如下所示: <my-component class="someClass"></my-component> …样式未应用于HTML。在Chrome Developer工具中,当我选择我的组件元素时,样式检查器显示正在应用样式,但从视觉上看,它们不是。这正常吗?我该如何解决这个问题呢?你为什么使用这种方法?您只需向my组件添加一些样式即可 CSS 当然,在组件中添加html代码为什么要使用这种方法?
<my-component class="someClass"></my-component>
…样式未应用于HTML。在Chrome Developer工具中,当我选择我的组件元素时,样式检查器显示正在应用样式,但从视觉上看,它们不是。这正常吗?我该如何解决这个问题呢?你为什么使用这种方法?您只需向
my组件添加一些样式即可
CSS
当然,在组件中添加html代码为什么要使用这种方法?您只需向my组件添加一些样式即可
CSS
当然,在组件中添加html代码如果要传递类名之类的内容,请使用绑定:
@Component({
selector: 'my-component',
template: template,
styleUrls: ['app/components/nav-bar/nav-bar.style.css'], // or you can add explicit style,
bindings:{
className: '<'
}
})
@组件({
选择器:“我的组件”,
模板:模板,
styleUrls:['app/components/nav bar/nav bar.style.css'],//或者您可以添加显式样式,
绑定:{
className:“如果要传递任何类似className的内容,请使用绑定:
@Component({
selector: 'my-component',
template: template,
styleUrls: ['app/components/nav-bar/nav-bar.style.css'], // or you can add explicit style,
bindings:{
className: '<'
}
})
@组件({
选择器:“我的组件”,
模板:模板,
styleUrls:['app/components/nav bar/nav bar.style.css'],//或者您可以添加显式样式,
绑定:{
类名:'Angular 2使用影子DOM,这是Web组件标准的一部分,支持DOM树和样式封装。
所以,如果你想在我的组件中设计一些样式,你应该在我的组件类中编写你的类
import { Component, OnInit } from '@angular/core';
@Component({
teamplte: '<h1 class="myClass">i am my-component</h1>',
styles: [`
.myClass {
someProp: value;
}
`]
})
export class ConatinerComponent implements OnInit {
constructor() { }
ngOnInit() { }
}
从'@angular/core'导入{Component,OnInit};
@组成部分({
团队成员:“我是我的组成部分”,
风格:[`
.myClass{
someProp:价值;
}
`]
})
导出类ConatinerComponent实现OnInit{
构造函数(){}
ngOnInit(){}
}
但若你们想在外部创建样式,你们必须在容器组件中编写你们的样式和类
import { Component, OnInit } from '@angular/core';
@Component({
teamplte: '<my-component class="myClass"></my-component>',
styles: [`
.myClass {
someProp: value;
}
`]
})
export class MyComponentComponent implements OnInit {
constructor() { }
ngOnInit() { }
}
从'@angular/core'导入{Component,OnInit};
@组成部分({
团队成员:'',
风格:[`
.myClass{
someProp:价值;
}
`]
})
导出类MyComponentComponent实现OnInit{
构造函数(){}
ngOnInit(){}
}
Angular 2使用Shadow DOM,这是Web组件标准的一部分,支持DOM树和样式封装。
所以,如果你想在我的组件中设计一些样式,你应该在我的组件类中编写你的类
import { Component, OnInit } from '@angular/core';
@Component({
teamplte: '<h1 class="myClass">i am my-component</h1>',
styles: [`
.myClass {
someProp: value;
}
`]
})
export class ConatinerComponent implements OnInit {
constructor() { }
ngOnInit() { }
}
从'@angular/core'导入{Component,OnInit};
@组成部分({
团队成员:“我是我的组成部分”,
风格:[`
.myClass{
someProp:价值;
}
`]
})
导出类ConatinerComponent实现OnInit{
构造函数(){}
ngOnInit(){}
}
但若你们想在外部创建样式,你们必须在容器组件中编写你们的样式和类
import { Component, OnInit } from '@angular/core';
@Component({
teamplte: '<my-component class="myClass"></my-component>',
styles: [`
.myClass {
someProp: value;
}
`]
})
export class MyComponentComponent implements OnInit {
constructor() { }
ngOnInit() { }
}
从'@angular/core'导入{Component,OnInit};
@组成部分({
团队成员:'',
风格:[`
.myClass{
someProp:价值;
}
`]
})
导出类MyComponentComponent实现OnInit{
构造函数(){}
ngOnInit(){}
}
发布组件装饰器部分,请发布组件装饰器部分,事实上,这对我在Chrome(或任何其他浏览器)上不起作用。我遇到的问题与我最初的问题相同:开发工具告诉我样式存在,但从视觉上看,它不存在。实际上,这对我在Chrome(或任何其他浏览器)上不起作用.Angular2在执行此操作时抛出一个错误:类型为“{selector:string;templateUrl:string;styleUrls:string[]”的参数;绑定:{className:string;};};}'不可分配给'Component'类型的参数。对象文字只能指定已知属性,并且'Component'类型中不存在'bindings'。执行此操作时,Angular2抛出一个错误:类型为{selector:string;templateUrl:string;styleUrls:string[];bindings:{className:string;};}的参数'不可分配给'Component'类型的参数。对象文字只能指定已知属性,并且'Component'类型中不存在'bindings'。