[英]Angularfire2 Property 'then' does not exist on type 'void'
On my app i'm setting an auth service. 在我的应用程序上,我正在设置身份验证服务。 Pretty simple behavior.
很简单的行为。
appComponent contains a simple button for login through Google with event (click)='login'
. appComponent包含一个简单的按钮,可通过事件
(click)='login'
通过Google (click)='login'
。 After i accept, I want to redirect my user to /account
. 我接受之后,我想将用户重定向到
/account
。
The app works as expected BUT i encounter an error on ng serve
: 该应用程序按预期工作, 但在
ng serve
上遇到错误:
[default] C:\\Project\\src\\app\\app.component.ts:24:8
[默认] C:\\ Project \\ src \\ app \\ app.component.ts:24:8
Property 'then' does not exist on type 'void'.
[default] C:\\Project\\src\\app\\services\\auth.service.ts:28:22
[默认] C:\\ Project \\ src \\ app \\ services \\ auth.service.ts:28:22
These errors appears to be non blocking, since the app appears to work properly. 这些错误似乎无阻塞,因为该应用程序似乎可以正常运行。 Do you have any idea of how to solve them?
您对如何解决它们有任何想法吗?
I guess the problem seems to appear because i apply then on the returned _auth.login() result which is the angularfire auth login method WHICH returns a promise... But i don't know how i can refactor my code to call the service properly if it's indeed the problem. 我猜问题似乎出现了,因为我随后在返回的_auth.login()结果上应用,这是angularfire auth登录方法WHICH返回了一个诺言...但是我不知道如何重构我的代码以调用服务如果确实是问题的话,那就正确。 I think (correct me if I'm wrong) it's a bad idea to call redirect from the service itself.
我认为(如果我错了,请纠正我)从服务本身调用重定向是个坏主意。
Here are my files. 这是我的文件。
app.component.ts: app.component.ts:
constructor(private router: Router, private _auth: AuthService) {
this.date = new Date();
}
login() {
this._auth.login()
.then(() => {
this.router.navigate(['/account']);
});
}
logout() {
this._auth.logout()
.then(() => {
this.router.navigate(['/login']);
});
}
My AuthService: 我的AuthService:
import { Injectable } from '@angular/core';
import { AngularFire, AuthProviders } from 'angularfire2';
@Injectable()
export class AuthService {
user = {};
isAuthenticated: boolean = false;
constructor(public af: AngularFire) {
this.af.auth.subscribe(user => {
if (user && !user.auth.isAnonymous) {
this.user = user;
this.isAuthenticated = true;
console.log('User Logged in', user, this.getUserEmail());
} else {
this.user = {};
this.isAuthenticated = false;
console.log('User Not Authenticated', user);
}
});
}
getUser() {
return this.user;
}
getUserEmail() {
return this.user.auth.email;
}
login() {
return this.af.auth.login({
provider: AuthProviders.Google
});
}
logout() {
return this.af.auth.logout();
}
}
Do you have any example of an authservice using AF properly? 您是否有使用AF的authservice的任何示例?
AngularFire2's logout
method doesn't return a Promise : AngularFire2的
logout
方法不会返回Promise :
public logout(): void {
this._authBackend.unauth();
}
There is an unmerged PR to have logout
return a Promise, but at the moment, it doesn't. 有一个未合并的PR ,可以使
logout
Promise,但目前还没有。
You could either change your app.component.ts
code to: 您可以将
app.component.ts
代码更改为:
logout() {
this._auth.logout();
this.router.navigate(['/login']);
}
Or you could change your AuthService
to: 或者,您可以将
AuthService
更改为:
logout() {
this.af.auth.logout();
return Promise.resolve();
}
Either way, you will need to watch to see if/when the PR is merged. 无论哪种方式,您都需要注意查看是否合并PR。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.