0

在我的 angular2 项目中,我使用 grunt 来自动编译我的打字稿文件。

一切都很好我的文件正在编译但总是出错

app/webapp/ng2/audit_logs/auditLogs.ts(2,3): error TS2304: Cannot find name 'app'.
app/webapp/ng2/audit_logs/auditLogs.ts(3,3): error TS2304: Cannot find name 'http'.
app/webapp/ng2/audit_logs/auditLogs.ts(3,10): error TS2304: Cannot find name 'ng'.
app/webapp/ng2/audit_logs/auditLogs.ts(6,3): error TS2304: Cannot find name 'app'.
app/webapp/ng2/audit_logs/auditLogs.ts(7,5): error TS2304: Cannot find name 'ng'.
app/webapp/ng2/main.ts(16,24): error TS2304: Cannot find name 'upgradeAdapter'.

我尝试过Definetlytyped,但没有成功。

我不知道为什么?

请帮我解决这个问题,main.ts and auditLogs.ts看看我的文件,让我知道哪里做错了

Main.ts 文件

/// <reference path="../../../node_modules/angular2/typings/browser.d.ts" />


var window: any = {
    giddh: undefined
};

import {UpgradeAdapter} from 'angular2/upgrade';

var adapter = new UpgradeAdapter();
var app = window.giddh.webApp;

adapter.bootstrap(document.body, ['giddhWebApp']);

// downgrade ng2 components to ng1 directives
app.directive('myApp', upgradeAdapter.downgradeNg2Component(app.AppComponent));

auditLogs.ts 文件

(function(giddh: any) {
  app = giddh.webApp
  http = ng.http


  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',
      templateUrl: ''
    }).Class({
      constructor: function(http: any) {
        this.test = new test(1020);
      }
    });

  class test {
    val: string;
    constructor(data: any){
      this.val = data;
    }
  }

})(window.giddh || (window.giddh = {}));

var window: any = {
  giddh: undefined
}
declare var require: any;
import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '/public/webapp/ng2/audit_logs/audit-logs.html'
})

export class AppComponent { 
}
4

1 回答 1

1

以下在多个级别上是错误的

(function(giddh: any) {
  app = giddh.webApp
  http = ng.http


  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',
      templateUrl: ''
    }).Class({
      constructor: function(http: any) {
        this.test = new test(1020);
      }
    });

  class test {
    val: string;
    constructor(data: any){
      this.val = data;
    }
  }

})(window.giddh || (window.giddh = {}));

var window: any = {
  giddh: undefined
}

你的文件是一个模块。你不需要咖啡。而且你不需要使用window. 更多:https ://basarat.gitbooks.io/typescript/content/docs/project/modules.html

你也不能http全局使用。你必须让 Angular 为你注入它。

ng仅用于类型信息,在运行时实际上并不存在。

于 2016-04-06T06:49:45.577 回答