【问题标题】:ReferenceError: gapi is not defined, but I tried everythingReferenceError:未定义gapi,但我尝试了一切
【发布时间】:2021-10-26 07:21:22
【问题描述】:

这听起来像是一个愚蠢的问题,因为有很多问题都与完全相同的问题有关,但没有一个几乎可以提供帮助。我正在使用 angular 并且在 ngAfterViewInit() 中我有以下代码:gapi.load("auth2", () => console.log("test")。我已经通过 npm 安装了 @types/gapi 和 @types/gapi.auth2。在我的 tsconfig.app.json 中,我在 compilerOptions 的类型中包含了“gapi”和“gapi.auth2”。同样在组件的 html 字段中,我将<script src="https://apis.google.com/js/api.js"></script> 放在了头部。我已经尝试将declare var gapi: any; 写入导入,但它也不起作用。我还尝试了三重 slas 指令 /// <reference path="../../../node_modules/@types/gapi/index.d.ts">/// <reference types="gapi">,但它们都不起作用。我不知道该怎么做,因为我尝试了在数小时的研究中能找到的一切。在此先感谢:)

【问题讨论】:

    标签: angular typescript google-api-js-client


    【解决方案1】:

    尝试使用动态导入:

    import("https://apis.google.com/js/api.js").then(() => {
      gapi.load("client:auth2", () => {console.log("success")});
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2021-05-04
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多