【发布时间】:2016-08-21 09:52:14
【问题描述】:
我的代码如下所示:
export class CRListComponent extends ListComponent<CR> implements OnInit {
constructor(
private router: Router,
private crService: CRService) {
super();
}
ngOnInit():any {
this.getCount(new Object(), this.crService.getCount);
}
ListComponent 的代码是这样的
@Component({})
export abstract class ListComponent<T extends Listable> {
protected getCount(event: any, countFunction: Function){
let filters = this.parseFilters(event.filters);
countFunction(filters)
.subscribe(
count => {
this.totalItems = count;
},
error => console.log(error)
);
}
来自 CRService 的相应服务代码片段是这样的:
getCount(filters) {
var queryParams = JSON.stringify(
{
c : 'true',
q : filters
}
);
return this.createQuery(queryParams)
.map(res => res.json())
.catch(this.handleError);
}
现在当我的ngOnInit() 运行时,我得到一个错误:
angular2.dev.js:23925 例外:类型错误:无法读取属性 'createQuery' of undefined in [null]
原始异常: TypeError:无法读取未定义的属性“createQuery”
所以基本上,return this.createQuery(queryParams) 语句中的this 将为空。有谁知道这怎么可能?
【问题讨论】:
-
你认为
this.createQuery为什么存在? -
您的 2 个班级缺少右括号是否“正常”?
-
我只是省略了不必要的部分,否则代码在语法上是正确的。 :) 问题不在于
.createQuery,而在于this本身。
标签: javascript angular angular2-services