typescript - typescript 中的@符号是什么意思--Angular 2

标签 typescript angular

我正在使用 typescript 进行 angular 2 应用程序开发。

但是当我们为组件或路由配置或其他一些地方编写代码时 我们使用“@”符号。

我的问题是这个符号是什么意思,为什么需要它?

最佳答案

您所指的 @ 符号称为 decorator

Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.

基本上,当您执行 @component 时,您是在告诉编译器该类是一个 angular2 组件,其元数据作为参数传递。

例如。

@Component({
  moduleId: module.id,
  selector: 'heroes-app',
  templateUrl: 'heroes.component.html',
  styleUrls: ['heroes.component.css'],  
})
class HeroesComponent{}

这段代码会告诉编译器类 HeroesComponent 应该是一个带有元数据作为参数传递的 angular2 组件,它会创建一个组件类。

Decorator 不是魔法。这只是函数调用。

例如。

@Component({
selector: 'static-component',
template: `<p>Static Component</p>`
})
class StaticComponent {}

相当于:

class DynamicComponent {
}

const dynamicComponent = Component({
    selector: 'dynamic-component',
    template: `<p>Dynamic Component</p>`
})(DynamicComponent);

希望这对您有所帮助。

关于typescript - typescript 中的@符号是什么意思--Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170004/

相关文章:

node.js - 在 sequelize.js 中获取与 belongsToMany 的关联

typescript - 扩展名为 '.tsx' eslint(react/jsx-filename-extension) 的文件中不允许使用 JSX

angular - typescript : Colon vs Equal To ( Angular Tutorial )

angular - 错误 Angular 13 : ngForOf not used by any directive. ..无法绑定(bind)到 'ngForOf'

javascript - 如何将焦点设置在 Angular 2 下拉列表的第一个列表项上

javascript - 无法读取未定义的属性 'input'

reactjs - iframe 的 useRef 使用什么类型

angular - ionic 地理定位插件错误 : "No provider for Geolocation"

angular - 改变 Angular Material 2 中的原色

javascript - 隐藏未输入的结果的搜索栏