【问题标题】:ES6 equivalent of this Angular 2 typescript这个 Angular 2 打字稿的 ES6 等价物
【发布时间】:2016-04-06 07:48:24
【问题描述】:

我有这个打字稿,我想编写 ES6 等价物。我正在学习 Angular 2,并且更喜欢使用 ES6 而不是 typescript,并且所有示例都在 ES5 或 typescript 中。如果我看到这段代码是如何用 ES6 编写的,那么我应该能够从那里使用我需要基于 typescript 编写的任何新代码来获取它。干杯。

'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';
  }
}

【问题讨论】:

标签: javascript typescript ecmascript-6 angular


【解决方案1】:

装饰器(例如@Component)是active EcmaScript 7 proposal。通过使用 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);

稍微清理一下,但不是 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;

当然,由于这一切还只是在提案阶段,未来装饰器的语法或语义可能会发生变化,因此您不一定要永远依赖它们以这种方式工作。

(另请注意,ES6 模块中的“use strict”编译指示没有意义;ES6 模块中的所有代码都已经按照规范在严格模式下运行。)

【讨论】:

    【解决方案2】:

    您的代码的 ES6 等效项位于 this plunk。我通过添加一个服务来演示 parameters 属性(见下文),稍微更改了您的代码。

    说明

    我认为你不知道如何将装饰器和类型转换为 ES6。

    1. 要替换类装饰器(例如ComponentDirective),请将annotation 属性添加到组件。您可以为此使用静态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. 要替换参数装饰器(例如@Inject)和类型(constructor(type: Type)),请将parameters 属性添加到组件。同样,您可以为此使用静态 getter

      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]];
      

    【讨论】:

    • 对像我这样的人的注意事项:尽管像上面的 Service 这样的依赖项被包装在他们自己的数组中,OpaqueTokens 不会。现在我知道了……
    猜你喜欢
    • 2020-10-29
    • 2017-01-19
    • 2016-04-19
    • 2015-10-06
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多