简体   繁体   English

与Angular 2打字稿等效的ES6

[英]ES6 equivalent of this Angular 2 typescript

I have this typescript and I want to write the ES6 equivalent. 我有这个打字稿,我想写等效的ES6。 I'm learning angular 2 and would prefer to use ES6 over typescript, and all the samples are in either ES5 or typescript. 我正在学习angular 2,并且宁愿使用ES6而不是打字稿,并且所有示例都在ES5或打字稿中。 If I see how this code is written in ES6 then I should be able to take it from there with any new code I need to write based off of typescript. 如果我看到此代码是如何在ES6中编写的,那么我应该可以从打字稿中获取任何需要编写的新代码,并将其从那里获取。 Cheers. 干杯。

'use strict';
import {Component, bootstrap} from 'angular2/angular2';

// Annotation section
@Component({
  selector: 'my-app',
  template: '<h1>Hello {{ name }}</h1>'
})
// Component controller
class MyApp {
  constructor() {
    this.name = 'Max';
  }
}

ES6 equivalent of your code is in this plunk . ES6相当于你的代码是在这个普拉克 I've changed your code a little bit by adding a service to demonstrate a parameters property (see below). 我通过添加服务来演示parameters属性(请参见下文),对您的代码进行了一些更改。

Explanation 说明

I think you don't know how to convert decorators and types into ES6. 我认为您不知道如何将装饰器和类型转换为ES6。

  1. To replace class decorators (such as Component and Directive ) add annotation property to a component . 要替换类装饰器(例如ComponentDirective ),请向组件添加批注属性。 You can use static getter for this: 您可以使用静态的getter为此:

     class App { static get annotations() { return [ new Component({ selector: 'my-app', template: '<h1>Hello, {{ name }}</h1>', providers: [Service] }) ]; } // ... } // or just add `annotation` after class declaration App.annotations = [ new Component({ selector: 'my-app', // ... }) ]; 
  2. To replace parameter decorators (such as @Inject ) and types ( constructor(type: Type) ) add parameters property to a component. 要替换参数修饰符(例如@Inject )和类型( constructor(type: Type) ),请向组件添加parameters属性。 Again you can use static getter for this: 同样,您可以为此使用静态吸气剂

     class App { // ... static get parameters() { return [[Service]]; } constructor(service) { this.name = service.getName(); setTimeout(() => this.name = 'Max', 1000); } } // or just add `parameters` after class declaration App.parameters = [[Service]]; 

Decorators (eg @Component ) are an active EcmaScript 7 proposal . 装饰器(例如@Component )是有效的EcmaScript 7建议 You can see the “equivalent” ES6 code by compiling your code with the TypeScript compiler and looking at the output: 通过使用TypeScript编译器编译代码并查看输出,可以看到“等效” ES6代码:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Component, bootstrap } from 'angular2/angular2';
// Annotation section
let MyApp = class {
    constructor() {
        this.name = 'Max';
    }
};
MyApp = __decorate([
    Component({
        selector: 'my-app',
        template: '<h1>Hello {{ name }}</h1>'
    })
], MyApp);

A slightly cleaned up, but not 100% equivalent to TypeScript output, would be: 稍微清理一下但不是100%等效于TypeScript输出的是:

import { Component, bootstrap } from 'angular2/angular2';
// Annotation section
let MyApp = class {
    constructor() {
        this.name = 'Max';
    }
};
MyApp = Component({
    selector: 'my-app',
    template: '<h1>Hello {{ name }}</h1>'
})(MyApp) || MyApp;

Of course since all of this is still just at the proposal phase, it is possible that the syntax or semantics of decorators will change in the future, so you can't necessarily rely on them working this way forever. 当然,由于所有这一切仍处于提议阶段,装饰器的语法或语义将来可能会更改,因此您不一定要永远依靠它们以这种方式工作。

(Please also note that a 'use strict' pragma inside an ES6 module makes no sense; all code inside ES6 modules already runs in strict mode per the specification.) (还请注意,ES6模块内的“使用严格”编译指示没有任何意义;根据规范,ES6模块内的所有代码已按严格模式运行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM