Angular-如何导入googleapi库



我正在尝试将Google Drive API集成到我的一个项目中。Node.js快速入门指南要求我导入文件读取和googleapi库。然而,当我尝试这样做时,我在将其导入app.module.ts或任何组件时遇到了问题:组件进口代码:

import { Injectable } from '@angular/core';
import {google, drive_v3} from 'googleapis'
const drive = google.drive({version: 'v3'});
@Injectable({
providedIn: 'root'
})
export class SubscriptionService {
constructor () {}
}

错误:

ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in '...node_modulesgoogle-auth-librarybuildsrcauth'
ERROR in ./node_modules/google-auth-library/build/src/crypto/node/crypto.js
Module not found: Error: Can't resolve 'crypto' in '...node_modulesgoogle-auth-librarybuildsrccryptonode'
ERROR in ./node_modules/jwa/index.js
Module not found: Error: Can't resolve 'crypto' in '...node_modulesjwa'
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'fs' in '...node_modulesgoogle-auth-librarybuildsrcauth'
ERROR in ./node_modules/google-p12-pem/build/src/index.js
Module not found: Error: Can't resolve 'fs' in '...node_modulesgoogle-p12-pembuildsrc'
ERROR in ./node_modules/googleapis-common/build/src/discovery.js
Module not found: Error: Can't resolve 'fs' in '...node_modulesgoogleapis-commonbuildsrc'
ERROR in ./node_modules/gtoken/build/src/index.js
Module not found: Error: Can't resolve 'fs' in '...node_modulesgtokenbuildsrc'
ERROR in ./node_modules/agent-base/patch-core.js
Module not found: Error: Can't resolve 'https' in '...node_modulesagent-base'
ERROR in ./node_modules/https-proxy-agent/index.js
Module not found: Error: Can't resolve 'net' in '...node_moduleshttps-proxy-agent'
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'os' in '...node_modulesgoogle-auth-librarybuildsrcauth'
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'path' in '...node_modulesgoogle-auth-librarybuildsrcauth'
ERROR in ./node_modules/gaxios/build/src/gaxios.js
Module not found: Error: Can't resolve 'stream' in '...node_modulesgaxiosbuildsrc'
ERROR in ./node_modules/google-auth-library/build/src/auth/oauth2client.js
Module not found: Error: Can't resolve 'stream' in '...node_modulesgoogle-auth-librarybuildsrcauth'
ERROR in ./node_modules/googleapis-common/build/src/apirequest.js
Module not found: Error: Can't resolve 'stream' in '...node_modulesgoogleapis-commonbuildsrc'
ERROR in ./node_modules/jws/lib/data-stream.js
Module not found: Error: Can't resolve 'stream' in '...node_modulesjwslib'ERROR in ./node_modules/jws/lib/sign-stream.js
Module not found: Error: Can't resolve 'stream' in '...node_modulesjwslib'ERROR in ./node_modules/jws/lib/verify-stream.js
Module not found: Error: Can't resolve 'stream' in '...node_modulesjwslib'ERROR in ./node_modules/https-proxy-agent/index.js
Module not found: Error: Can't resolve 'tls' in '...node_moduleshttps-proxy-agent'

如何在Angular中正确导入googleapi库?正如许多在线用户所建议的那样,我已经尝试将以下脚本导入添加到index.html中,但它似乎不起作用(请参阅评论(。

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

我还确保执行以下安装:

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

googleapis(和google-auth-library(是一个NodeJS库。你不能在前端项目中按原样使用它。有一些解决方法-请参阅https://medium.com/angular-in-depth/google-apis-with-angular-214fadb8fbc5(但我没有亲自尝试,所以不能推荐(
你提到的另一个库-https://apis.google.com/js/api.js-是正确的,它用于前端,但它不是模块化的,它只是一个需要通过脚本标记加载的大脚本。这是因为您无法通过import导入它。为了让TS编译器满意(如果您已经安装了类型@types/gapi*(,请在访问gapi全局对象的文件中添加此导入:

/// <reference types="gapi.client. drive" />

最新更新