Angular 2 @Component 语法是 ES6 的一部分吗?

Is the Angular 2 @Component syntax part of ES6?

谁能告诉我。导入的Component函数前面的'@'符号。那是 ES6 语法吗?我还没有看到它用于我看过的任何其他非 angular ES6 项目。

import {Component} from ...
@Component({})

Here is a example

简答:否

@ 语法定义了一个注释 - 这是由 Angular 的 AtScript, which later merged into TypeScript 引入的。据我所知,它们类似于 .NET 语言中的注释。

注解不是标准 ES6 的一部分;它们只是 TypeScript 提供的一种装饰。值得注意的是,Angular 2 支持使用 TypeScript 注释,Aurelia 也是如此。

目前我无法提供 link,但是有一些资源非常详细地描述了 ES6 的特性和语言组件。

@ 语法是 es7 新草案的一部分,目前处于第 1 阶段(提案阶段)。它们被称为装饰器。

Decorators make it possible to annotate and modify classes and properties at design time.

有关详细信息,请参阅:https://github.com/wycats/javascript-decorators


注意:您可以使用装饰器,通过启用 optional[]=es7.decorators(在 webpack 中)或将您的配置设置为 stage:1

来使用 Babel

仅作记录,只需将 TypeScript 注释转换为以下内容,即可在 ES6 中实现相同的行为:

import {Component, ...} from 'angular2/core';
export class myAppOrDirective {
    static get annotations() {
        return [
            new Component({
                selector: 'my-app-or-directive'
            }),
            new View({
                template: `<div>Hello!</div>`
            })
        ];
    }
}