简体   繁体   English

使用Angular JS将打字稿用于ES6支持

[英]Using typescript for ES6 support, with Angular JS

I am looking for a transpiler that gives me the following:- 我正在寻找可以为我提供以下内容的编译器:

  • Use ES6 syntax for classes, "let" scoping, fat arrow syntax 对类使用ES6语法,“让”作用域,粗箭头语法
  • Some support for imports (ideally ES6 proposed syntax) 对导入的某些支持(理想情况下是ES6建议的语法)
  • Support for IE8 (that is, compiles down to ES3) 支持IE8(即编译为ES3)
  • Compatibility with AngularJS 与AngularJS的兼容性
  • Compatibility with gulp 与口香糖的相容性
  • Compile all my code into a single file without requirejs/AMD. 无需requirejs / AMD将我的所有代码编译到一个文件中。

Now, most of that is covered by TypeScript, but typescript comes with its own type syntax which I prefer not using as it seems to require me to download/setup interfaces/definition files. 现在,大多数内容都由TypeScript覆盖,但是Typescript带有自己的类型语法,我不喜欢使用它,因为它似乎需要我下载/设置接口/定义文件。

I have had a play with it and I have had not much success. 我玩过它,但没有取得太大的成功。 You can see the code below. 您可以在下面看到代码。 Can someone either please suggest an alternative (and no, I don't want to use Dart), or tell me what I am doing wrong. 有人可以建议一个替代方法(不,我不想使用Dart),或者告诉我我做错了什么。

app.ts (my "main" file) app.ts(我的“主”文件)

/// <reference path="MyController.ts"/>
/// <reference path="MyService.ts"/>
// <reference path="../bower_components/angular/angular.min.js"/>

angular.module("test")
      .controller("MyController", MyController.injections)
      .factory("MyService", MyService.injections);

MyController.ts MyController.ts

/// <reference path="MyService.ts"/>
module MyController {
    class MyController {
        constructor(MyService) {
            this.MyService = MyService;
        }

        callSomething() {
            this.MyService.doSomething();
        }
    }
    export var injections = [ "MyService", MyController ];
}

MyService.ts MyService.ts

module MyService {
    export class MyService {
        constructor($http) {

        }

        doSomething() {
            console.log("Yo");
        }
    }
    export var injections = [ "$http", MyService ];
}

Here are the issues the typescript compiler's output:- 以下是打字稿编译器输出的问题:

C:\dev\temp\angulargulp>gulp compile
[gulp] Using gulpfile C:\dev\temp\angulargulp\gulpfile.js
[gulp] Starting 'compile'...
[gulp] Finished 'compile' after 3.87 ms
[gulp] Compiling TypeScript files using tsc version 1.0.1.0
[gulp] [tsc] > error TS5023: Unknown option 'allowimportmodule'
[gulp] [tsc] > C:\dev\temp\angulargulp\app\scripts\MyController.ts(5,9): error TS2094: The property 'MyService' does not exist on value of type 'MyController'.
[gulp] [tsc] > Use the '--help' flag to see options.
[gulp] [tsc] > C:\dev\temp\angulargulp\app\scripts\MyController.ts(9,9): error TS2094: The property 'MyService' does not exist on value of type 'MyController'.
[gulp] [tsc] > C:\dev\temp\angulargulp\app\scripts\app.ts(5,1): error TS2095: Could not find symbol 'angular'.
[gulp] Failed to compile TypeScript: Error: tsc command has exited with code:1

events.js:72
        throw er; // Unhandled 'error' event
              ^
[←[32mgulp←[39m] Error in plugin '←[36mgulp-tsc←[39m': Failed to compile: tsc command has exited with code:1
    at C:\dev\temp\angulargulp\node_modules\gulp-tsc\index.js:51:33
    at C:\dev\temp\angulargulp\node_modules\gulp-tsc\lib\compiler.js:326:8
    at Array.forEach (native)
    at Function.Compiler._allAborted (C:\dev\temp\angulargulp\node_modules\gulp-tsc\lib\compiler.js:325:13)
    at Function.Compiler.abortAll (C:\dev\temp\angulargulp\node_modules\gulp-tsc\lib\compiler.js:303:14)
    at C:\dev\temp\angulargulp\node_modules\gulp-tsc\index.js:50:20
    at C:\dev\temp\angulargulp\node_modules\gulp-tsc\lib\compiler.js:110:7
    at Transform.<anonymous> (C:\dev\temp\angulargulp\node_modules\gulp-tsc\lib\compiler.js:205:5)
    at Transform.EventEmitter.emit (events.js:117:20)
    at C:\dev\temp\angulargulp\node_modules\gulp-tsc\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:942:16

I think this reference: 我认为这个参考:

// <reference path="../bower_components/angular/angular.min.js"/>

should actually be to a .d.ts type definition file for Angular. 实际上应该是Angular的.d.ts类型定义文件。


In this class: 在本课程中:

class MyController {
    constructor(MyService) {
        this.MyService = MyService;
    }

you try to use this.MyService but you never declared it. 您尝试使用this.MyService但从未声明过。

class MyController {
    MyService: MyService.MyService;

    constructor(MyService) {
        this.MyService = MyService;
    }

-or- you can automatically make the constructor arguments into class variables like this: -或者-您可以像下面这样自动将构造函数参数设置为类变量:

class MyController {

    constructor(private MyService: MyService.MyService) {
    }

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

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