角度ngrx / store authguard用户属性未定义问题

时间:2018-09-26 09:08:27

标签: angular store ngrx auth-guard

我正在构建一个使用ngrx / store进行状态管理的angular应用程序,并且其中有一个用户对象作为状态的一部分。

用户类别

export class User {
    constructor (
        public email: string
    ) {}

    get is_signed_in(): boolean {
        return this.email !== '';
    }
}

export class User { constructor ( public email: string ) {} get is_signed_in(): boolean { return this.email !== ''; } }

AuthGuard:

export class AuthGuard implements CanActivate {
    private user$: Observable<User>;
    constructor(
        private service: UserService,
        private router: Router
    ) {
        this.user$ = service.user$;
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return Observable.create((observer: NextObserver<boolean>) => {
            this.user$.subscribe((user: User) => {
                console.log('authguard', user);
                console.log('authguard', user.email);
                console.log('user.is_signed_in', user.is_signed_in);
                if (!user.is_signed_in) {
                    console.log('redirecting to signin page');
                    this.router.navigate(
                        ['user', 'signin'],
                        {
                            queryParams: {
                                url: state.url
                            }
                        }
                    );
                }

                observer.next(user.is_signed_in);
                observer.complete();
            });
        });
    }
}

,但是在authguard类中,user.is_signed_in属性未定义。为什么?

0 个答案:

没有答案