2016-03-26 73 views
4

鑑於下面的腳本,可以在angular 2 official tutorial中找到,「@」字符表示什麼?它是一個ecmascript 6功能嗎?ecmascript 6中的「@」char是什麼意思?

任何人都可以詳細說明嗎?

import {Component} from 'angular2/core'; 
export class Hero { 
    id: number; 
    name: string; 
} 
@Component({ 
    selector: 'my-app', 
    template:` 
    <h1>{{title}}</h1> 
    <h2>{{hero.name}} details!</h2> 
    <div><label>id: </label>{{hero.id}}</div> 
    <div> 
     <label>name: </label> 
     <input [(ngModel)]="hero.name" placeholder="name"> 
    </div> 
    ` 
}) 
export class AppComponent { 
    public title = 'Tour of Heroes'; 
    public hero: Hero = { 
    id: 1, 
    name: 'Windstorm' 
    }; 
} 
+1

@用於指示裝飾和anotation之前有提名 –

+0

參見HTTP://blog.thoughtram .io/angular/2015/05/03/the-difference-between-annotations-and-decorators.html和http://stackoverflow.com/questions/29775830/how-to-implement-a-typescript-decorator –

回答

1

這只是打字稿的裝飾,檢查出來here

A類裝飾就是一個類聲明之前聲明。類裝飾器應用於類的構造器,並且可以使用 來觀察,修改或替換類定義。類 修飾符不能用於聲明文件或任何其他 環境上下文中(例如在聲明類上)。

類裝飾器的表達式將作爲 運行時的函數調用,裝飾類的構造函數僅作爲其參數 。

如果類裝飾器返回一個值,它將用提供的構造函數替換類 聲明。

注意如果您選擇返回新的構造函數,則必須注意保持原始原型。運行時應用 裝飾器的邏輯將不會爲您執行此操作。以下是一類裝飾(@sealed)施加到Greeter類的 例如:

@sealed 
class Greeter { 
    greeting: string; 
    constructor(message: string) { 
     this.greeting = message; 
    } 
    greet() { 
     return "Hello, " + this.greeting; 
    } 
}