无法读取 属性 未定义的内容 Angular

Cannot read property something of undefined Angular

我不确定我正在尝试做的事情是否符合逻辑,但请尝试一下。

我正在尝试在我的 Angular 应用程序中添加 Veriff SDK。 我得到以下错误。任何帮助表示赞赏。

.html

<button class="btn" [disabled]="isVerified"
                    (click)="myFn()">Finish</button>

.ts 文件

export class myComponent(){
   isVerified : boolean = true;

ngOnInit() {

  const veriff = Veriff({
      host: 'https://stationapi.veriff.com',
      apiKey: '',
      parentId: 'veriff-root',
      onSession: function(err, response) {
        console.log(response.status)  //success
      window.veriffSDK.createVeriffFrame({ url: response.verification.url }); 
      if( response && response.status === 'success'){
        this.isVerified = !this.isVerified
      }
.....} //rest of the code is here

在 ngOnInit() 上,我调用 Veriff api。

如果响应成功,我正在尝试将禁用设置为 false。但是我得到了这个 Cannot read 属性 'isVerified' of undefined。如何解决这个问题。

你必须使用箭头函数(lambda 表达式)。否则如果你使用 functions 它将改变 this.

的含义

这应该可以解决您的问题:

ngOnInit() {

  const veriff = Veriff({
      host: 'https://stationapi.veriff.com',
      apiKey: '',
      parentId: 'veriff-root',
      onSession: (err, response) => {
        console.log(response.status)  //success
      window.veriffSDK.createVeriffFrame({ url: response.verification.url }); 
      if( response && response.status === 'success'){
        this.isVerified = !this.isVerified
      }
.....} //rest of the code is here