@angular装饰器总结

本文详细介绍了Angular中各类装饰器的使用方法,包括NgModule、Component、Directive、Pipe等类装饰器,以及Input、Output、HostBinding等属性装饰器,帮助开发者更好地理解和应用Angular框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类装饰

装饰NgModule

@NgModule({ 
  providers?: Provider[]
  declarations?: Array<Type<any> | any[]>
  imports?: Array<Type<any> | ModuleWithProviders | any[]>
  exports?: Array<Type<any> | any[]>
  entryComponents?: Array<Type<any> | any[]>
  bootstrap?: Array<Type<any> | any[]>
  schemas?: Array<SchemaMetadata | any[]>
  id?: string
})
复制代码

装饰Component

@Component({ 
  changeDetection?: ChangeDetectionStrategy
  viewProviders?: Provider[]
  moduleId?: string
  templateUrl?: string
  template?: string
  styleUrls?: string[]
  styles?: string[]
  animations?: any[]
  encapsulation?: ViewEncapsulation
  interpolation?: [string, string]
  entryComponents?: Array<Type<any> | any[]>
  preserveWhitespaces?: boolean
  selector?: string
  inputs?: string[]
  outputs?: string[]
  host?: {...}
  providers?: Provider[]
  exportAs?: string
  queries?: {...}
})
复制代码

装饰Directive

@Directive({ 
  selector?: string
  inputs?: string[]
  outputs?: string[]
  host?: {...}
  providers?: Provider[]
  exportAs?: string
  queries?: {...}
})
复制代码

装饰Pipe

@Pipe({ 
  name: string
  pure?: boolean
})
复制代码

装饰 Injectable

@Injectable({ 
  providedIn?: Type<any> | 'root' | null
  factory: () => any
})
复制代码

属性装饰

  • 属性获取
@Attribute({ 
  attributeName?: string
})
复制代码
  • 宿主绑定
@HostBinding({ 
  hostPropertyName?: string
})
复制代码
  • 宿主事件绑定
@HostListener({ 
  eventName?: string
  args?: string[]
})
复制代码
  • 输入
@Input({ 
  bindingPropertyName?: string
})
复制代码
  • 输出
@Output({ 
  bindingPropertyName?: string
})
复制代码
  • 单个内容查询
@ContentChild()
复制代码
  • 多个内容查询
@ContentChildren()
复制代码
  • 单个视图查询
@ViewChild()
复制代码
  • 多个视图查询
@ViewChildren()
复制代码

参数装饰

  • 顺序检索
@Host()
复制代码
  • 可空不报错,检索不到时为null
@Optional()
复制代码
  • 只在本身检索
@Self()
复制代码
  • 略过自身检测
@SkipSelf()
复制代码
  • 用来注入被标记Injectable的类
@Inject({ 
  token: any
})
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值