打字稿类,而变量的访问
我在打字稿打字稿类,而变量的访问
class SlowQueriesController {
$http
constructor($http, $routeParams) {
this.$http = $http;
..... some other code ....
this.$http.get('/api/metrics/findByType/' + $routeParams.application + '/METER').then(response => {
this.meters = response.data;
for (var i = 0; i < this.meters.length; i++) {
fillInMeterData(this.meters[i]);
}
});
}
function fillInMeterData(meter) {
this.$http.get('/api/meterData/values/' + meter._id).then(response => {
... some code ...
});
}
}
该类我得到的问题是,在方法fillInMeterData我在哪里访问本该是在方法定义。$ HTTP,这是不确定的。
任何想法我做错了什么。请原谅我打字稿的知识浅薄,我仍在学习。
我发现这个问题,但问题是,有哪些不正确声明fillInMeterData之前的方法(它们被声明如“功能方法名( )')
一旦我修复了类中的所有方法,以遵循语法,一切正常。
在打字稿类的方法不应该包含function
关键字,并用它在this playground of a stripped version of your code编译器应该抱怨你,如:
class SlowQueriesController {
constructor($http, $routeParams) { }
function fillInMeterData(meter) {} // ERROR: Unexpected token. A constructor, method, accessor, or property was expected.
}
您的代码应该是这样的:
class SlowQueriesController {
$http
constructor($http, $routeParams) {
this.$http = $http;
..... some other code ....
this.$http.get('/api/metrics/findByType/' + $routeParams.application + '/METER').then(response => {
this.meters = response.data;
for (var i = 0; i < this.meters.length; i++) {
this.fillInMeterData(this.meters[i]);
}
});
}
fillInMeterData(meter) {
this.$http.get('/api/meterData/values/' + meter._id).then(response => {
... some code ...
});
}
}
区别在于:
- 否
function
关键字的fillInMeterData
方法 - 调用此方法时,使用
this.fillInMeterData
我试过,但我在编译器中得到这个错误,然后“无法lint:app/slowQueries/slowQueries.controller.ts [102,25]:缺少分号。”.......行号102是我在哪里有方法fillInMeterData –
这不是一个编译器错误,但一个lint错误。你使用哪种棉绒?你能用产生这个错误的确切代码更新你的问题吗? –
我如何知道我正在使用哪种棉绒?基本上我使用yeoman generator-angular-fullstack生成基本结构并根据我的用法进行修改。粘贴整个代码会非常复杂,如果需要,我可以通过电子邮件向您发送代码。 –
你怎么能在课堂上有一个功能?编译器应该抱怨。 –
它并没有抱怨我,你会建议我的替代方案吗? –