gpt4 book ai didi

angular - Angular 6 中的谷歌身份验证

转载 作者:太空狗 更新时间:2023-10-29 19:32:46 24 4
gpt4 key购买 nike

我在 Angular 6 中创建了一个项目,它使用 angular-6-social-login 展示了谷歌身份验证.以下是安装命令:

npm install --save angular-6-social-login

在此之后,我对 app.module.ts 文件进行了以下更改:

import {SocialLoginModule,AuthServiceConfig,GoogleLoginProvider} from "angular-6-social-login";


// Configs
export function getAuthServiceConfigs() {
let config = new AuthServiceConfig(
[
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider("Your-Google-Client-Id")
}
];
);
return config;
}

@NgModule({
imports: [
...
SocialLoginModule
],
providers: [
...
{
provide: AuthServiceConfig,
useFactory: getAuthServiceConfigs
}
],
bootstrap: [...]
})


export class AppModule { }

以及 app.component.ts 中的以下更改:

import {Component, OnInit} from '@angular/core';
import {
AuthService,
GoogleLoginProvider
} from 'angular-6-social-login';

@Component({
selector: 'app-signin',
templateUrl: './signin.component.html',
styleUrls: ['./signin.component.css']
})


export class SigninComponent implements OnInit {

constructor( private socialAuthService: AuthService ) {}

public socialSignIn(socialPlatform : string) {
let socialPlatformProvider;
if(socialPlatform == "facebook"){
socialPlatformProvider = FacebookLoginProvider.PROVIDER_ID;
}else if(socialPlatform == "google"){
socialPlatformProvider = GoogleLoginProvider.PROVIDER_ID;
}else if (socialPlatform == "linkedin") {
socialPlatformProvider = LinkedinLoginProvider.PROVIDER_ID;
}

this.socialAuthService.signIn(socialPlatformProvider).then(
(userData) => {
console.log(socialPlatform+" sign in data : " , userData);
// Now sign-in with userData
// ...

}
);
}

}

以下是我的app.component.html

<button (click)="socialSignIn('google')">Sign in with Google</button> 

我启动了应用程序,它运行良好。虽然我在控制台中收到以下错误:

Error that the console displays when i start the application. Basically when i run ng serve --open and open the console window of the application

应用程序运行良好,即单击按钮时,会弹出一个谷歌登录屏幕,询问我的谷歌凭据。我输入它们并对其进行身份验证。

我有 3 个问题或疑问:1)为什么会出现这个错误?

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'signIn' 
of undefined
TypeError: Cannot read property 'signIn' of undefined
at angular-6-social-login.umd.js:250
at new ZoneAwarePromise (zone.js:891)
at GoogleLoginProvider.push../node_modules/angular-6-social-login/angular-6-
social-login.umd.js.GoogleLoginProvider.signIn (angular-6-social-
login.umd.js:249)

2) 我们导出了函数getAuthServiceConfigs(),随后在app.module.ts 的Providers 数组中声明了它。那有什么用?

3) 我需要在应用程序启动时呈现谷歌登录屏幕,即不点击按钮。我该如何实现?

我尝试调用 ngOnInit() 中的方法:

ngOnInit(){
this.socialSignIn('google');
}

但屏幕显示空白

请帮我解决这个问题!

最佳答案

这里的问题是这样的

ngOnInit(){
this.socialSignIn('google');
}

SigninComponent 的注入(inject) socialAuthServicengOnInit() 上不可用。你可以在Difference between Constructor and ngOnInit中看到更多

因为我在 app.component.html 上看到了一个按钮。如果您尝试模拟鼠标点击,我建议您使用适当的 Lifecycle Hooks

此外,删除 app.component.ts 中的 FacebookLoginProviderLinkedInLoginProvider,因为 app 中没有导入和提及。模块.ts

关于angular - Angular 6 中的谷歌身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com