Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

ionic framework - Typescript Error, Cannot find name 'gapi', transpile failed

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

There is two warnings:

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

npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

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

I installed

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

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

my code:

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'
          });
        });
      };

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...