Angular2 'this' 未定义
Angular2 'this' is undefined
我的代码如下所示:
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 EXCEPTION: TypeError: Cannot read property
'createQuery' of undefined in [null]
ORIGINAL EXCEPTION:
TypeError: Cannot read property 'createQuery' of undefined
所以基本上,return this.createQuery(queryParams)
语句中的 this
将为空。有人知道这怎么可能吗?
问题所在:
gOnInit():any {
this.getCount(new Object(), this.crService.getCount); // <----
}
由于您在对象外部引用了函数。您可以在其上使用 bind
方法:
this.getCount(new Object(), this.crService.getCount.bind(this.crService));
或者把它包装成一个箭头函数:
this.getCount(new Object(), (filters) => {
return this.crService.getCount(filters));
});
第二种方法是首选方法,因为它允许保留类型。有关详细信息,请参阅此页面:
为了修复这个错误,我从导致错误的函数中抽出所有内部结构并将其放入另一个函数中,然后错误就消失了。
示例:
我有这个函数,里面有一些代码
this.globalListenFunc = renderer.listenGlobal('document', 'click', (event) => {
// bunch of code to evaluate click event
// this is the code that had the "this" undefined error
});
我把代码拉出来放在一个外部的public函数中,这里是完成的代码:
this.globalListenFunc = renderer.listenGlobal('document', 'click', (event) => {
this.evaluateClick(event);
});
evaluateClick(evt: MouseEvent){
// all the code I yanked out from above
}
我的代码如下所示:
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 EXCEPTION: TypeError: Cannot read property 'createQuery' of undefined in [null]
ORIGINAL EXCEPTION: TypeError: Cannot read property 'createQuery' of undefined
所以基本上,return this.createQuery(queryParams)
语句中的 this
将为空。有人知道这怎么可能吗?
问题所在:
gOnInit():any {
this.getCount(new Object(), this.crService.getCount); // <----
}
由于您在对象外部引用了函数。您可以在其上使用 bind
方法:
this.getCount(new Object(), this.crService.getCount.bind(this.crService));
或者把它包装成一个箭头函数:
this.getCount(new Object(), (filters) => {
return this.crService.getCount(filters));
});
第二种方法是首选方法,因为它允许保留类型。有关详细信息,请参阅此页面:
为了修复这个错误,我从导致错误的函数中抽出所有内部结构并将其放入另一个函数中,然后错误就消失了。
示例:
我有这个函数,里面有一些代码
this.globalListenFunc = renderer.listenGlobal('document', 'click', (event) => {
// bunch of code to evaluate click event
// this is the code that had the "this" undefined error
});
我把代码拉出来放在一个外部的public函数中,这里是完成的代码:
this.globalListenFunc = renderer.listenGlobal('document', 'click', (event) => {
this.evaluateClick(event);
});
evaluateClick(evt: MouseEvent){
// all the code I yanked out from above
}