“无法读取未定义的属性'dialogService'” Angular 9 gapi函数

问题描述

我正在尝试打开一个对话框,其中包含从Google Api客户端(gapi)中检索到的数据。一切正常,直到我尝试将数据加载到对话框中。我收到的数据很好,我可以将数据传递到其他功能正常的对话框中,而没有问题,但是由于某些原因,我无法在gapi函数中完成此操作。

我正在Angular框架中使用Nebular的NbDialog。这是我的代码

import { Component,OnDestroy,OnInit,ElementRef } from '@angular/core';
import { FileListComponent } from '../../../pages/modal-overlays/dialog/file-list/file-list.component';
import { NbDialogService } from '@nebular/theme';

const gapi: any

@Component({
  selector: 'ngx-header',styleUrls: ['./header.component.scss'],templateUrl: './header.component.html',})


export class HeaderComponent implements OnInit,OnDestroy {
 
 constructor(private dialogService: NbDialogService){}
 
 ngOnInit(){

    //Initialize

    gapi.load("client:auth2",() =>{
      gapi.client.init({
        apiKey: 'xxxxxxxxxxxxxxx',discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'],clientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',scope: 'profile https://www.googleapis.com/auth/drive.readonly'
      }).then(function () {
        // Listen for sign-in state changes.
        // gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
        // Handle the initial sign-in state.
        if(gapi.auth2.getAuthInstance().isSignedIn.get()){
          console.log('Sign-in successful');
          gapi.client.drive.files.list({
            pageSize: 100,corpora:'allDrives',fields: "nextPagetoken,files(id,name)",includeItemsFromAllDrives:true,supportsAllDrives:true,q:"mimeType='application/vnd.google-apps.document'",}).then(function(res) {

            // console.log returns data perfectly fine

            console.log(res.result)
                 
             // below returns "Cannot read property 'dialogService' of undefined" even though I've used the
             // EXACT same function outside this one with no issues                
 
            this.dialogService.open(FileListComponent,{
              context: {
                list: res.result.files,},});
          });
        }
        else{
          console.log('there was an error with sign-in my friend')
        }

      });

    })
  }


感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)