如何从angular 10中的变量配置访问函数

如何从angular 10中的变量配置访问函数,angular,Angular,我有一个变量声明如下 export class AppComponent implements OnInit { // constructor() { } config = { // replace this key with yours publicKey: "test_public_key_dc74e0fd57cb46cd93832aee0a390234", productIdentity: "1234567890&quo

我有一个变量声明如下

    export class AppComponent implements OnInit {
  // constructor() { }
  config = {
    // replace this key with yours
    publicKey: "test_public_key_dc74e0fd57cb46cd93832aee0a390234",
    productIdentity: "1234567890",
    productName: "Drogon",
    productUrl: "http://gameofthrones.com/buy/Dragons",
    eventHandler: {
      onSuccess(payload) {
        // hit merchant api for initiating verfication
        console.log(payload);
        this.handleSuccessResponse(payload);
      },
      // onError handler is optional
      onError(error) {
        // handle errors
        console.log(error);
      },
      onClose() {
        console.log("widget is closing");
      }
    }
  };
  checkout = new KhaltiCheckout(this.config);

  ngOnInit() {}
  khaltiCheckout() {
    this.checkout.show({ amount: 1000 });
  }
  handleSuccessResponse(payload) {
    console.log(payload);
  }

  handleErrorResponse(payload) {
    console.log(payload);
  }
}
此处,已成功调用this.generateAlphaNumericValue(10),但函数this.handleSuccessResponse(payload)作为未定义函数抛出错误。 如何在成功事件中调用此.handleSuccessResponse(有效负载)。

此.successR(有效负载)
不在config的作用域中,因为内部函数的缘故,作用域丢失了,所以像这样更改它

export class AppComponent implements OnInit {
  constructor() {
    // Change
    var self = this;
    config = {
        // replace this key with yours
        publicKey: "test_public_key_dc74e0fd57cb46cd93832aee0a390234",
        productIdentity: "1234567890",
        productName: "Drogon",
        productUrl: "http://gameofthrones.com/buy/Dragons",
        eventHandler: {
          onSuccess(payload) {
            // Change
            self.successR(payload);
          },
          // onError handler is optional
          onError(error) {
            // handle errors
            console.log(error);
          },
          onClose() {
            console.log("widget is closing");
          }
        }
      };
      checkout = new KhaltiCheckout(this.config);
   }
}
this.successR(有效载荷)
不在config的作用域中,因为内部函数的缘故,作用域丢失了,所以像这样更改它

export class AppComponent implements OnInit {
  constructor() {
    // Change
    var self = this;
    config = {
        // replace this key with yours
        publicKey: "test_public_key_dc74e0fd57cb46cd93832aee0a390234",
        productIdentity: "1234567890",
        productName: "Drogon",
        productUrl: "http://gameofthrones.com/buy/Dragons",
        eventHandler: {
          onSuccess(payload) {
            // Change
            self.successR(payload);
          },
          // onError handler is optional
          onError(error) {
            // handle errors
            console.log(error);
          },
          onClose() {
            console.log("widget is closing");
          }
        }
      };
      checkout = new KhaltiCheckout(this.config);
   }
}

我可以看到一个代码,在哪里以及如何准确地调用
onSuccess
函数吗?直接传递函数引用,即
onSuccess=this.handleSuccessResponse“
或使用aror函数,即
onSuccess=>(有效负载)>{this.handleSuccessResponse(有效负载);},
@PankajParkar我已经更新了代码。你能检查一下吗。@Satpal是对的,你可以使用Arrow函数在
onSuccess
函数中保存组件类上下文。@Satpal-onSuccess是一个回调函数。我不能分配或使用Arrow函数。我必须从onSuccess函数中调用一个函数。我能看到一个代码吗这里以及您如何准确地调用
onSuccess
函数?直接传递函数引用,即
onSuccess=this.handleSuccessResponse“
或使用aror函数,即
onSuccess=>(有效载荷)>{this.handleSuccessResponse(有效载荷);},
@PankajParkar我已经更新了代码。您能检查一下吗。@Satpal是对的,您可以使用Arrow函数在
onSuccess
函数中保存组件类上下文。@Satpal-onSuccess是一个回调函数。我不能分配或使用箭头函数。我必须从onSuccess函数中调用函数。