在Angular 2+中,属性规范化规则是将属性名转换为小写,并将连字符(-)转换为驼峰命名法。这是为了与HTML属性的命名规则保持一致。
下面是一个示例代码,展示了属性规范化规则的应用:
// 在组件类中
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-custom-component',
template: 'My custom property value: {{ myCustomProperty }}'
})
export class CustomComponent {
@Input('my-custom-property') myCustomProperty: string;
}
在上面的示例中,组件的属性my-custom-property
将被规范化为myCustomProperty
,并在组件模板中使用。在组件类中,使用了@Input
装饰器来声明输入属性,并通过('my-custom-property')
指定了规范化后的属性名。
这样,当组件被使用时,可以通过my-custom-property
来设置属性值,并在组件内部使用myCustomProperty
来访问该属性。