广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

Component是Directive的子类,它是一个装饰器,用于把某个类标记为Angular组件。下面本篇文章就来带大家深入了解angular中的@Component装饰器,希望对大家有所帮助。
一、@Component 装饰器1.1 @Component 装饰器的用途
声明一个组件时,在组件类的之上要用@Component装饰器来告知Angular这是一个组件。【相关教程推荐:《angular教程》】
import { Component, OnInit } from '@angular/core';@Component({ selector: 'app-product-alerts', templateUrl: './product-alerts.component.html', styleUrls: ['./product-alerts.component.css']})export class ProductAlertsComponent implements OnInit { constructor() { } ngOnInit() { }}登录后复制1.2 @Component 装饰器的常用选项
@Component 装饰器继承于 Directive ,这个css选择器用于在模板中标记出该指令,并触发该指令的实例化。
1.2.1 继承自@Directive装饰器的选项
1.2.2 @Component自己特有的选项
selector 选择器可使用下列形式之一:
element-name: 根据元素名选取[attribute]: 根据属性名选取.class: 根据类名选取[attribute=value]: 根据属性名和属性值选取not(sub_selector): 只有当元素不匹配子选择器 sub_selector 的时候才选取selector1, selector2: 无论 selector1 还是 selector2 匹配时都选取2.1 element-name: 根据元素名选取
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})登录后复制<app-element></app-element>登录后复制
2.2 [attribute]: 根据属性名选取
@Component({ selector: '[app-element]', template: './element.component.html', styleUrls: ['./element.component.css']})登录后复制<div app-element></div>登录后复制
2.3 .class: 根据类名选取
@Component({ selector: '.app-element', template: './element.component.html', styleUrls: ['./element.component.css']})登录后复制<div class="app-element"></div>登录后复制登录后复制登录后复制登录后复制三、
host: {[key:string]:string}使用一组键-值对,把类的属性映射到宿主元素的绑定(Property、Attribute 和事件)。 Angular 在变更检测期间会自动检查宿主 Property 绑定。 如果绑定的值发生了变化,Angular 就会更新该指令的宿主元素。
当 key 是宿主元素的 Property 时,这个 Property 值就会传播到指定的 DOM 属性。当 key 是 DOM 中的静态 Attribute 时,这个 Attribute 值就会传播到宿主元素上指定的 Property 去。注意属性的值默认为变量,如果直接使用属性值,需要加字符串单引号或者双引号,变量直接在组件里定义即可对于事件处理:
它的 key 就是该指令想要监听的 DOM 事件。 要想监听全局事件,请把要监听的目标添加到事件名的前面。 这个目标可以是 window、document 或 body。它的 value 就是当该事件发生时要执行的语句。如果该语句返回 false,那么就会调用这个 DOM 事件的 preventDefault 函数。 这个语句中可以引用局部变量 $event 来获取事件数据。3.1 attribute 和 property
异同:
部分属性既属于 property ,又属于 attribute ,比如 idattribute 初始化后不会再改变; property 默认值为初始值,会随着 dom 更新所以在 angular2 中双向绑定实现是由 dom 的 property 实现的,所以指令绑定的是 property ,但是在某些情况下 dom 不存在某个 property 比如 colspan,rowspan 等,这时想要绑定 html 标签特性需要用到 attr:
<table width="100%" border="10px solid"> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td [attr.colspan]=colnum>January</td> </tr> <tr> <td [attr.colspan]=colnum>February</td> </tr></table>let colnum:number = 2;登录后复制
3.2 使用 host 绑定 class
@Component({ selector: '.app-element', template: './element.component.html', styleUrls: ['./element.component.css'], host: { '[class.default-class]': 'useDefault' }, encapsulation: ViewEncapsulation.None // 让宿主元素也用上 element.component.css 样式。否则,默认胶囊封装避免CSS污染。})export class AppElementComponent { @Input() useDefault = true;}登录后复制<div class="app-element"></div>登录后复制登录后复制登录后复制登录后复制
3.3 使用 host 绑定 style
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})0登录后复制<div class="app-element"></div>登录后复制登录后复制登录后复制登录后复制
3.4 使用 host 绑定事件
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})2登录后复制<div class="app-element"></div>登录后复制登录后复制登录后复制登录后复制四、
encapsulation(封装)供模板和 CSS 样式使用的样式封装策略。
4.1 Web Components
通过一种标准化的非侵入的方式封装一个组件,每个组件能组织好它自身的 HTML 结构、CSS 样式、JavaScript 代码,并且不会干扰页面上的其他元素。
Web Components 由以下四种技术组成:
Custom Elements: 自定义元素HTMLTemplates: HTML模板Shadow DOM: 影子DOMHTMLImports: HTML导入4.2 Shadow DOM
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})4登录后复制4.3 ViewEncapsulation
ViewEncapsulation 允许设置三个可选的值:
ViewEncapsulation.Emulated: 无 Shadow DOM,但是通过 Angular 提供的样式包装机制来封装组件,使得组件的样式不受外部影响。这是 Angular 的默认设置。ViewEncapsulation.ShadowDom: 使用原生的 Shadow DOM 特性ViewEncapsulation.None: 无 Shadow DOM,并且也无样式包装4.3.1 ViewEncapsulation.None
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})5登录后复制ViewEncapsulation.None 设置的结果是没有 Shadow DOM,并且所有的样式都应用到整个 document,换句话说,组件的样式会受外界影响,可能被覆盖掉。
4.3.2 ViewEncapsulation.Emulated
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})6登录后复制ViewEncapsulation.Emulated 设置的结果是没有 Shadow DOM,但是通过 Angular 提供的样式包装机制来封装组件,使得组件的样式不受外部影响。虽然样式仍然是应用到整个 document,但 Angular 为 .greet 类创建了一个 [_ngcontent-cmy-0] 选择器。可以看出,我们为组件定义的样式,被 Angular 修改了。其中的 _nghost-cmy- 和 _ngcontent-cmy- 用来实现局部的样式。
4.3.3 ViewEncapsulation.ShadowDom
@Component({ selector: 'app-element', template: './element.component.html', styleUrls: ['./element.component.css']})7登录后复制ViewEncapsulation.ShadowDom 设置的结果是使用原生的 Shadow DOM 特性。Angular 会把组件按照浏览器支持的 Shadow DOM 形式渲染。
更多编程相关知识,请访问:编程视频!!
以上就是深入了解angular中的@Component装饰器的详细内容,更多请关注9543建站博客其它相关文章!


评论列表
感觉不错!https://aptlawfirm.com/