类型'typeof client'上不存在'drive'

时间:2018-01-21 22:25:35

标签: angular google-drive-api

Angular 5无法从drive找到gapi.client。我所做的是:

package.json

"@types/gapi.client.drive": "^3.0.0",

tsconfig.app.json

"types": ["gapi.auth2", "gapi.client", "gapi.client.drive"]

index.html

<script src="https://apis.google.com/js/platform.js"></script>

我的编辑器(VS Code)和Angular都报告如下错误:

Property 'drive' does not exist on type 'typeof client'

,但console.log(gapi.client.drive.files);可以获得我需要的一切。

我在这里错过了任何图书馆吗?

3 个答案:

答案 0 :(得分:1)

确保安装所有这些插件。

npm install --save @types/gapi
npm install --save @types/gapi.auth2
npm install --save @types/gapi.client.drive

和tsconfig.app.json中 在编译器选项部分

"types": ["gapi", "gapi.auth2" ,"gapi.client", "gapi.client.drive"]

,并且需要在index.html文件中添加此js。

 <script src="https://apis.google.com/js/api.js"></script>

答案 1 :(得分:0)

解决Angular中gapigapi.auth这个问题的一种可能方法是使用NPM安装类型脚本定义。

npm install --save @types/gapi
npm install --save @types/gapi.auth2

tsconfig.json部分的tsconfig.app.jsoncompilerOptions中,将此"gapi", "gapi.auth2"添加到types。看起来像

"types": ["gapi", "gapi.auth2"],

然后在您的服务或组件中,使用NgZone,然后从@angular/core导入

以下是示例:

import { Component, NgZone, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-google-app',
  templateUrl: './google-app.component.html',
  styleUrls: ['./google-app.component.scss']
})
export class GoogleAppComponent implements OnInit, AfterViewInit {

constructor(private ngZone: NgZone) { }

ngAfterViewInit() {
    this.ngZone.run(() => {
        // example to render login button
        gapi.signin2.render('my-signin2', {
        ...
        ...
        });

        // example to load client
        gapi.load('client', {
         ...
         ...
        });

    });
  }    
}

Angular NgZone文档... read more

index.html 中,根据需要,您可能需要在<head></head>标记内添加以下内容

<meta name="google-signin-client_id" content="xxxxxxxxxx.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>
<script src="https://apis.google.com/js/api.js"></script>

答案 2 :(得分:0)

安装@types/gapi.client.sheets为我解决了这个问题

相关问题