所有人都在尝试使用angular5-social-login做角度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();
}
}
答案 0 :(得分:1)
您的问题的临时解决方案
ngAfterViewInit()
{
setTimeout (()=>{this.googlelogin()}, 1000);
}