简体   繁体   English

如何在没有装饰器的情况下将 Angular2 与 ES6 一起使用?

[英]How to use Angular2 with ES6 without decorators?

Following the ES5 quickstart on the Angular 2 website I get something along the lines of:按照Angular 2网站上的 ES5 快速入门,我得到了以下内容:

mycomponent =
    ng.core.Component({
        ...
    })
    .Class({
        constructor: function() {}
    });

I am wondering how I can swap out the .Class method for a proper ES6 class but without using decorators (as they are not yet part of an ES standard)我想知道如何将 .Class 方法换成适当的ES6 class但不使用decorators (因为它们还不是 ES 标准的一部分)

You could use a getter for the annotations property with the ComponentMetadata class, as described below:您可以对ComponentMetadata类的annotations属性使用 getter,如下所述:

import {ComponentMetadata,Inject} from 'angular2/core';
import {FormBuilder} from 'angular2/common';

export class AppComponent {
  constructor(fb) {
    console.log(fb);
  }

  static get parameters() {
    return [[FormBuilder]];
  }

  static get annotations() {
    return [
      new ComponentMetadata({
        selector: 'my-app',
        template: `
          <div>
            Test
          </div>
        `
      })
    ];
  }
}

See this plunkr: https://plnkr.co/edit/VqDSDuroK29iAxGzA5Lc?p=preview看到这个 plunkr: https ://plnkr.co/edit/VqDSDuroK29iAxGzA5Lc ? p = preview

You can notice that tools like Babel support decorators.你会注意到像 Babel 这样的工具支持装饰器。

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

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