1.4 注解Annotation - RLwu/angular-start GitHub Wiki
注解/Annotation
你一定好奇@Component和@View到底是怎么回事。看起来像其他语言(比如python) 的装饰器,是这样吗?
ES6规范里没有装饰器。这其实利用了转码器提供的一个特性:注解。给一个类 加注解,等同于设置这个类的annotations属性:
//注解写法
@Component({selector:"ez-app"})
class EzApp{...}
等同于:
class EzApp{...}
EzApp.annotations = [new Component({selector:"ez-app"})];
很显然,注解可以看做转码器(typescript/traceur/babel)层面的语法糖,但和python 的装饰器不同,注解在编译转码时仅仅被放在类对象的annotation属性里,编译器并不进 行解释展开 —— 这个解释的工作是Angular2框架完成的:
据称,注解/Annotation的功能就是Angular2团队向编译器开发团队提出的,因此这通常不是 编译器的默认选项,因此你 看到, 我们配置systemjs在使用TypeScript转码时打开注解:
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
});