ReferenceError: gapi is not defined, but I tried everything

ReferenceError: gapi is not defined, but I tried everything

这听起来像是一个愚蠢的问题,因为有大量关于完全相同问题的问题,但其中 none 几乎可以提供帮助。我正在使用 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">,但其中 none 有效。我不知道该怎么做,因为我尝试了数小时研究中能找到的一切。提前致谢:)

尝试使用动态导入:

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