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

笑着哭i 提交于 2019-12-10 19:22:43

问题


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

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? without success.

  • How do I get rid of the IIFE and use ES6 syntax?

回答1:


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.

You can check it also in a working example.




回答2:


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?

Just to be clear you don't need to reference app, you just need to import AppComponent (as you already have)



来源:https://stackoverflow.com/questions/34397310/what-do-i-need-to-convert-my-angular2-component-to-es6-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!