打字稿错误,找不到名称“gapi",转译失败 [英] Typescript Error, Cannot find name 'gapi', transpile failed

查看:38
本文介绍了打字稿错误,找不到名称“gapi",转译失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 ionic2 应用程序中实现 google auth.我需要它在浏览器中工作.所以,我安装了:

I trying to implement google auth in my ionic2 app. I need it works in browser. So, I installed:

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

有两个警告:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0(node_moduleschokidar ode_modulesfsevents):

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_moduleschokidar ode_modulesfsevents):

npm 警告 notsupSKIPPING OPTIONAL DEPENDENCY:不支持的平台fsevents@1.0.15:想要 {"os":"darwin","arch":"any"}(当前:{"os":"win32","arch":"x64"})

npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.0.15: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

在 node_modules 文件夹中,我有 gapi 和 gapi.auth2 文件夹,但我有打字稿错误:找不到名称gapi",转换失败.

In node_modules folder I have gapi and gapi.auth2 folders but I have typescript error: Cannot find name 'gapi', transpile failed.

我安装了

npm install typings -g
typings install dt~gapi --global --save
typings install dt~gapi.auth2 --global --save

仍然出现同样的错误:找不到名称'gapi',编译失败

Still have the same error: Cannot find name 'gapi', transpile failed

我的代码:

auth2: any;

login() {
      gapi.load('auth2', () => {
          this.auth2 = gapi.auth2.init({
             client_id: 'xxxxxxxxx.apps.googleusercontent.com',
             scope: 'https://www.googleapis.com/auth/userinfo.email'
          });
        });
      };

我的 package.json:

my package.json:

"@angular/core": "2.2.1",
"ionic-angular": "2.0.0-rc.4",
"ionic-native": "2.2.11",
"rxjs": "5.0.0-beta.12",
"typescript": "2.0.9"

推荐答案

gapi对于 gapi.auth2 是全局声明.

The current declarations for gapi and for gapi.auth2 are global declarations.

它们可能不会自动与 @types 一起使用,因为查看 node_modules 要么需要显式 import 或将您的库添加到 tsconfig.json 中的 types 字段.

They might not be automatically with @types because looking into node_modules either requires an explicit import or adding your libraries to the types field in your tsconfig.json.

因此尝试将以下 types 字段修复到您的 compilerOptions 中:

So try fixing up the following types field to your compilerOptions:

"compilerOptions": {
     "types": ["gapi", "gapi.auth2"]
}

这篇关于打字稿错误,找不到名称“gapi",转译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆