Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 5导入Google gapi witn@types/gapi.auth2不工作_Angular_Typescript_Google Signin_Googlesigninapi - Fatal编程技术网

Angular 5导入Google gapi witn@types/gapi.auth2不工作

Angular 5导入Google gapi witn@types/gapi.auth2不工作,angular,typescript,google-signin,googlesigninapi,Angular,Typescript,Google Signin,Googlesigninapi,我在Angular 5中有一个项目,我正在尝试使用Google gapi.auth2库实现登录。我被这个错误所困扰: ERROR in ./src/app/signin.service.ts Module not found: Error: Can't resolve 'gapi.auth2' in 'C:\Users\Javier\workspace\angular-tour-of-heroes\src\app' 该错误似乎与我在服务中导入库的方式有关。 我的实现包括通过npm安装-save

我在Angular 5中有一个项目,我正在尝试使用Google gapi.auth2库实现登录。我被这个错误所困扰:

ERROR in ./src/app/signin.service.ts
Module not found: Error: Can't resolve 'gapi.auth2' in 'C:\Users\Javier\workspace\angular-tour-of-heroes\src\app'
该错误似乎与我在服务中导入库的方式有关。 我的实现包括通过npm安装-save@types/gapi.auth2将@types/gapi.auth2:0.0.47添加到package.json文件。将import gapi.auth2添加到signin.service.ts,这是我试图从中调用gapi的角度服务

package.json

签名服务:

index.html

在travis中可以看到全部错误: 或者从这里克隆回购协议: 并运行npm启动

你能帮我找出我做错了什么吗


非常感谢。

好的,解决问题的方法是将gapi.auth2添加到tsconfig.app.json中

{ 扩展:../tsconfig.json, 编译器选项:{ outDir:../out tsc/app, baseUrl:./, 模块:es2015, 类型:[gapi.auth2] }, 排除:[ test.ts, **/*.spec.ts ] } 您可以在此处看到正在运行的构建:


非常感谢

您在索引文件中导入了google api库吗?是的,我导入了,我刚刚在问题中添加了index.html。非常感谢。
{
 "name": "angular-tour-of-heroes",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.5",
    "@angular/cdk": "^5.2.1",
    "@angular/common": "^5.2.5",
    "@angular/compiler": "^5.2.5",
    "@angular/core": "^5.2.5",
    "@angular/forms": "^5.2.5",
    "@angular/http": "^5.2.5",
    "@angular/material": "^5.2.1",
    "@angular/platform-browser": "^5.2.5",
    "@angular/platform-browser-dynamic": "^5.2.5",
    "@angular/router": "^5.2.5",
    "@types/gapi.auth2": "0.0.47",
    "angular-in-memory-web-api": "^0.5.2",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.6.3",
    "@angular/compiler-cli": "^5.2.5",
    "@angular/language-service": "^5.2.5",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  }
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { fromPromise } from 'rxjs/observable/fromPromise';
import { catchError, map, tap, mergeMap } from 'rxjs/operators';
import "gapi.auth2";

import { MessageService } from "./message.service";
import { Profile } from './profile';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable()
export class SigninService {
  private signinUrl = 'api/login/google';  // URL to web api
  constructor(
    private http: HttpClient,
    private messageService: MessageService) {
      gapi.load('auth2', () => {
        gapi.auth2.init({
          client_id: '204566820246-67tser74gtv78uiaskm945enn5b5agl2.apps.googleusercontent.com',
          scope: 'profile email'
        });
      });
  }

  public signIn(): Observable<Profile> {
    return this.http.get<Profile>('/api/user').pipe(
      catchError(_ => {return this.signInGoogle()})
    )
  }

  signInGoogle(): Observable<Profile> {
    var auth2 = gapi.auth2.getAuthInstance();
    return fromPromise(auth2.grantOfflineAccess()).pipe(
      mergeMap((resp: any)  => { return this.signInBackEnd(resp.code);})
    );
  }

  private signInBackEnd(code: string): Observable<Profile> {
    let params = new HttpParams()
    .append('code', code)
      .append('redirect_uri', 'http://localhost:4200');

    return this.http.get<Profile>(this.signinUrl, {params: params}).pipe(
      mergeMap<any, Profile>(_ => {return this.http.get<Profile>('/api/user')}),
      tap(_ => this.log(`singin success`)),
      catchError(this.handleError<Profile>('signin'))
    );
  }

  public signOut(): Observable<Profile> {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
      console.log('User signed out.');
    });
    return this.http.get<Profile>('/api/logout').pipe(
      tap(_ => this.log(`sign out success`)),
      catchError(this.handleError<Profile>('signout'))
    );
  }

  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead
      // TODO: better job of transforming error for user consumption
      this.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }

  /** Log a HeroService message with the MessageService */
  private log(message: string) {
    this.messageService.add('HeroService: ' + message);
  }

}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="google-signin-client_id" content="204566820246-67tser74gtv78uiaskm945enn5b5agl2.apps.googleusercontent.com">
  <title>AngularTourOfHeroes</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <script src="https://apis.google.com/js/platform.js"></script>
</head>
<body>
  <app-root></app-root>
</body>
</html>