【发布时间】:2023-04-04 22:13:01
【问题描述】:
所有人都在尝试使用 angular5-social-login 在 angular 7 中授权 gmail。无论如何它对我有用,但我需要在页面加载时运行该功能。我尝试在ngOnInit 和ngAfterViewInit 中调用它,但出现以下错误:
未捕获(承诺中):TypeError:无法读取未定义的属性“signIn” TypeError:无法读取未定义的属性“signIn”
import { Component, OnInit, NgZone } from '@angular/core';
import { AuthService, SocialLoginModule } from 'angular5-social-login';
import { GoogleLoginProvider } from 'angular5-social-login'
import { User } from 'src/app/commen/user';
import { CookieService } from 'ngx-cookie-service';
import { HttpClient } from '@angular/common/http';
import { ApiService } from 'src/app/api.service';
import { Router } from '@angular/router'
import { AppComponent } from '../app.component';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
constructor(
public SocialAuthService: AuthService,
private cookie: CookieService,
private http: HttpClient,
private _ApiService: ApiService,
private _user: User,
private router: Router,
private app: AppComponent,
public zone: NgZone
) {
if (this.cookie.get('email') != '') {
this.router.navigate(['\home']);
}
}
googlelogin() {
let socialPlatformProvider = GoogleLoginProvider.PROVIDER_ID;
this.SocialAuthService.signIn(socialPlatformProvider).then((userdata) => {
if (userdata.email.includes('nagwa.com')) {
this._ApiService.getRole(userdata.email).subscribe((res) => {
this.cookie.set('email', userdata.email, 12)
this.cookie.set('name', userdata.name, 12);
if (res == "admin") {
this.cookie.set('role', 'true', 12)
}
else this.cookie.set('role', 'false', 12)
this.cookie.set('image', userdata.image, 12)
this.router.navigate(['\home'])
}
)
}
else alert("please check with nagwa account")
}).catch(function (err) {
console.log(err)
});
}
ngOnInit() {
}
ngAfterViewInit()
{
this.googlelogin();
}
}
【问题讨论】:
-
从错误看来,SocialAuthService 为空或未定义
-
是的,它返回 null 我想念一些调用 auth2 的东西,当我尝试从事件调用时,但是当我从单击按钮调用时它工作? @AnkurShah
-
尝试在 ngAfterViewInit 中设置 1000 毫秒的超时。如果它有效,那么您需要找到一个有效的地方。
-
当我添加设置超时 @AnkurShah 时它可以工作,但我尝试所有页面事件而不是一项工作
标签: angular gmail-api angular7