在我的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 {
}
答案 0 :(得分: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
}
您的文件是一个模块。你不需要iffee。而且您不需要使用window
。更多:https://basarat.gitbooks.io/typescript/content/docs/project/modules.html
此外,您无法全局使用http
。你必须让角度为你注入它。
同样ng
仅用于类型信息,并且在运行时实际上并不存在。