![](/img/trans.png)
[英]Angular 2.0 : Does Typescript support all the features of ES6?
[英]Using typescript for ES6 support, with Angular JS
我正在尋找可以為我提供以下內容的編譯器:
現在,大多數內容都由TypeScript覆蓋,但是Typescript帶有自己的類型語法,我不喜歡使用它,因為它似乎需要我下載/設置接口/定義文件。
我玩過它,但沒有取得太大的成功。 您可以在下面看到代碼。 有人可以建議一個替代方法(不,我不想使用Dart),或者告訴我我做錯了什么。
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
/// <reference path="MyService.ts"/>
module MyController {
class MyController {
constructor(MyService) {
this.MyService = MyService;
}
callSomething() {
this.MyService.doSomething();
}
}
export var injections = [ "MyService", MyController ];
}
MyService.ts
module MyService {
export class MyService {
constructor($http) {
}
doSomething() {
console.log("Yo");
}
}
export var injections = [ "$http", MyService ];
}
以下是打字稿編譯器輸出的問題:
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
我認為這個參考:
// <reference path="../bower_components/angular/angular.min.js"/>
實際上應該是Angular的.d.ts
類型定義文件。
在本課程中:
class MyController {
constructor(MyService) {
this.MyService = MyService;
}
您嘗試使用this.MyService
但從未聲明過。
class MyController {
MyService: MyService.MyService;
constructor(MyService) {
this.MyService = MyService;
}
-或者-您可以像下面這樣自動將構造函數參數設置為類變量:
class MyController {
constructor(private MyService: MyService.MyService) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.