简体   繁体   English

Angularfire2属性“ then”在“ void”类型上不存在

[英]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.

相关问题 类型'Observable <{}>'上不存在属性'update'。 Firebase 5 AngularFire2 5 - Property 'update' does not exist on type 'Observable<{}>'. Firebase 5 AngularFire2 5 AngularFire2 类型:“类型‘FirebaseObjectObservable’上不存在属性‘take’<any> &#39;&quot; - AngularFire2 typings: "Property 'take' does not exist on type 'FirebaseObjectObservable<any>'" &#39;QueryFn&#39;类型中不存在&#39;query&#39;| angularfire2 | - ‘query’ does not exist in type ‘QueryFn’ | angularfire2 | 类型“ Appcomponent”上不存在Firebase和AngularFire2&#39;afAuth&#39; - Firebase & AngularFire2 'afAuth' does not exist on type 'Appcomponent' 类型为forEach的属性在类型void上不存在 - property of type forEach does not exist on type void 类型诺言中不存在属性映射<void> - property map does not exist on type promise<void> &#39;PromiseLike&#39;类型中不存在属性&#39;catch&#39; <void> “ - Property 'catch' does not exist on type 'PromiseLike<void>' 类型 void 上不存在 Angular 属性订阅 - Angular Property subscribe does not exist on type void 然后,属性在void类型上不存在,打字稿错误 - property then does not exist on type void , A typescript error “void”类型上不存在属性“管道”Angular 6 - Property 'pipe' does not exist on type 'void' Angular 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM