简体   繁体   English

如何为用Es6编写的angularjs 2设置脚本标签以移植到Es5

[英]How to set up script tags for angularjs 2 written in Es6 for transpilation to Es5

Below is from a plunker that started from the angularjs 2 es6 template http://goo.gl/IGaTZw I added traceur and removed the main.es6.js replacing with main.js in case the implicit mode was interfering. 下面是一个从plunker开始的插件,该插件从angularjs 2 es6模板http://goo.gl/IGaTZw开始。

<head>
<script data-require="traceur-runtime@0.0.88" data-semver="0.0.88" src="https://cdn.rawgit.com/google/traceur-compiler/d3d0553de3315398a956dc2f9edd6a982d786b0a/bin/traceur-runtime.js"></script>
<script src="https://jspm.io/system.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.30/angular2.dev.js"></script>
</head>    

<body>
<app></app>
<script>System.import('main');</script>
</body>

Main.js----- Main.js -----

import {
  ComponentAnnotation as Component,
  ViewAnnotation as View, bootstrap
} from 'angular2/angular2';

@Component({
  selector: 'app',
  viewInjector: [Service]
})
@View({
  template: '{{greeting}} world!'
})
class App {
  constructor(service: Service) {
    this.greeting = service.greeting();
    setTimeout(() => this.greeting = 'Howdy,', 1000);
  }

} }

class Service {
  greeting() {
    return 'Hello';
  } 
}

bootstrap(App);

I tried to do locally having downloaded script files but failed there as well. 我尝试在本地下载脚本文件,但在那里也失败了。 I cannot use npm etc as have an issue with my laptop. 我无法使用npm等,因为笔记本电脑有问题。

Many thanks in advance. 提前谢谢了。

To rectify, include traceur options: 要进行纠正,请包含traceur选项:

 System.config({
   traceurOptions: {
     annotations: true
 }

, ( annotations are experimental and have to be turned on manually ) add ,(注释是实验性的,必须手动打开)添加

import { Inject } from 'angular2/di';

and change the App constructor argument 并更改App构造函数参数

constructor(@Inject(Service) service).

It is also seems possible to have the type in the constructor constructor( service:Service) dispensing with the inject annotation, as long as you add types:true to the traceur options. 只要您在traceur选项中添加type:true,似乎也可以在构造函数的builder(service:Service)中取消注入类型的类型。

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

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