角8:编译器“找不到名称'gapi'”

时间:2019-07-31 21:56:29

标签: typescript angular8 gapi

在我被标记为重复之前,我已经尝试过解决非常相似的问题here的问题,并将tsconfig.json中comiplerOptions中的类型更新为this solution

基本上,我已经创建了一个Angular服务(Angular 8,这就是为什么以前的解决方案可能不起作用的原因),该服务调用了Google SignIn的Google Api(gapi)。

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private router: Router) { }

  gapi: any; //solution from link one which allows me to run it locally

  googleInit() {
    gapi.load('auth2', () => {
      gapi.auth2.init({
        ...
      })
    });
  }
  ... (other gapi calls)

}

即使编译器发出错误消息,此服务在我的本地服务器(localhost:4200)上也能正常工作。

 ERROR in src/app/shared/services/user/user.service.ts(25,12): error TS2663: Cannot find name 'gapi'. Did you mean the instance member 'this.gapi'?

但是,当我尝试运行npm run build将其部署在服务器上时,编译器崩溃并显示“ This is probably not a problem with npm. There is likely additional logging output above.

日志显示为

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/usr/local/Cellar/node/12.4.0/bin/node',
1 verbose cli   '/usr/local/bin/npm',
1 verbose cli   'run',
1 verbose cli   'build'
1 verbose cli ]
2 info using npm@6.10.2
3 info using node@v12.4.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle pos@0.0.0~prebuild: pos@0.0.0
6 info lifecycle pos@0.0.0~build: pos@0.0.0
7 verbose lifecycle pos@0.0.0~build: unsafe-perm in lifecycle true
8 verbose lifecycle pos@0.0.0~build: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/jho/TzuChi/taurus/flaskr/static/pos/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
9 verbose lifecycle pos@0.0.0~build: CWD: /Users/jho/TzuChi/taurus/flaskr/static/pos
10 silly lifecycle pos@0.0.0~build: Args: [ '-c', 'ng build' ]
11 silly lifecycle pos@0.0.0~build: Returned: code: 1  signal: null
12 info lifecycle pos@0.0.0~build: Failed to exec build script
13 verbose stack Error: pos@0.0.0 build: `ng build`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:326:16)
13 verbose stack     at EventEmitter.emit (events.js:200:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:200:13)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
14 verbose pkgid pos@0.0.0
15 verbose cwd /Users/jho/TzuChi/taurus/flaskr/static/pos
16 verbose Darwin 18.7.0
17 verbose argv "/usr/local/Cellar/node/12.4.0/bin/node"    "/usr/local/bin/npm" "run" "build"
18 verbose node v12.4.0
19 verbose npm  v6.10.2
20 error code ELIFECYCLE
21 error errno 1
22 error pos@0.0.0 build: `ng build`
22 error Exit status 1
23 error Failed at the pos@0.0.0 build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

非常感谢您的任何反馈。

1 个答案:

答案 0 :(得分:0)

结果是我错误地实现了第一个链接。答案是在服务之外声明变量gapi: any

declare var gapi: any; //this is the key

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private router: Router) { }
  ... 
}