Angular 4,当使用ng2-smart表的回调'onComponentInitFunction'函数时,'this'是未定义的 [英] Angular 4 , 'this' is undefined when using callback 'onComponentInitFunction' function of ng2-smart table
问题描述
我在组件中使用ng2-smart-table.
I am using ng2-smart-table in my component.
我正在尝试调用父组件的 approveTheOrder()方法,但是我无法获取父类的对象
I am trying to invoke the parent component's approveTheOrder() method but I am unable to get the object of the parent class
下面是代码段
{
title: "Actions",
type: "custom",
renderComponent: ActionRenderComponent,
onComponentInitFunction(instance) {
instance.actionEmitter.subscribe(row => {
if(row == CONSTANT.STATUS_APPROVE){
//here 'row' value is from childComponent-ActionRenderComponent,which I am able to get easily in parentComponet
this.approveTheOrder(instance.rowData.id); // here this is undefined,
}
if(row == CONSTANT.STATUS_REJECT){
this.rejectOrder();//this is undefined
}
if(row == CONSTANT.STATUS_PENDING){
this.pendingOrder(); // this is undefined
}
});
},
filter: false
};
有人知道如何在下面的onComponentInitFunction()中获取"this"吗?
Does anyone have any idea how to get the 'this' in the below onComponentInitFunction() ?
下面是我得到的错误的图像.
Below is the image of the error that I am getting.
我也尝试使用'bind'功能未能成功实现目标,有人可以在这里指导我,我真的被困在这一点上.
Also I tried to use 'bind' function was unsuccessful in achieving the goal, Could anyone please guide me here, I am really stuck at this point.
编辑1 请注意,我能够从 parentComponent 中的 ChildComponent 获取事件,但是这里的问题特定于ng2-smart-table组件.
EDIT 1 Note that I am able to get event from ChildComponent in the parentComponent, but the problem here is specific to ng2-smart-table component.
要从 ChildComponent 获取值,我尝试使用ng2-smart-table组件的内置回调函数 onComponentInitFunction(instance)
To get the value from ChildComponent, I am trying to use in-build callback function onComponentInitFunction(instance) of ng2-smart-table component
推荐答案
以下语法:
{
onComponentInitFunction(instance) {
...
将被转换为函数表达式:
will be transformed to function expression:
{
onComponentInitFunction: function(instance) {
...
您应使用箭头功能保留this
:
You should use arrow function to retain this
:
onComponentInitFunction: (instance) => {
这篇关于Angular 4,当使用ng2-smart表的回调'onComponentInitFunction'函数时,'this'是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!