简体   繁体   English

将Angular2组件转换为ES6语法需要什么?

[英]What do I need to convert my Angular2 component to ES6 syntax?

index.js index.js

Here is my entry point 这是我的切入点

import * as stylesheet from '../assets/styles/app.scss';

import jQuery from '../node_modules/jquery/dist/jquery';
import $ from '../node_modules/jquery/dist/jquery';

import {bootstrap}    from './boot'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

app.component.js app.component.js

This works 这有效

(function (app) {
    app.AppComponent = ng.core
        .Component({
            selector: 'my-app',
            template: '<h1>My First Angular 2 App</h1>'
        })
        .Class({
            constructor: function () {
            }
        });
})(window.app || (window.app = {}));

Question

I tried to use answer from How do I write angular2 without decorator syntax? 我尝试使用如何在没有装饰器语法的情况下编写angular2的答案 without success. 没有成功。

  • How do I get rid of the IIFE and use ES6 syntax? 如何摆脱IIFE并使用ES6语法?

Untested! 未经测试!

import {Component} from 'angular2/core';

@Component({
  selector: 'my-app',
  template: `
    <h1>
        My First Angular 2 App
    </h1>
  `
})
export class AppComponent {
  constructor () {

  }
}

Perhaps you got a little confused by thinking you needed the app variable? 也许你认为你需要app变量有点困惑?

Just to be clear you don't need to reference app , you just need to import AppComponent (as you already have) 为了清楚你不需要引用app ,你只需要导入AppComponent (就像你已经拥有的那样)

If you are using Babel and use the following plugins: 'angular2-annotations', 'transform-decorators-legacy', 'transform-class-properties', 'transform-flow-strip-types' there is no need to convert the syntax. 如果您正在使用Babel并使用以下插件: 'angular2-annotations', 'transform-decorators-legacy', 'transform-class-properties', 'transform-flow-strip-types' ,则无需转换语法。

You can check it also in a working example . 您也可以在一个工作示例中查看它。

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

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