简体   繁体   English

Angular2 DataStore-使用rxjs Subject时authGuards不再起作用

[英]Angular2 DataStore - authGuards no longer not working when using rxjs Subject

I am building a User Profile Data Store so my Angular 2 app doesn't need to go to the server so frequently. 我正在构建一个用户个人资料数据存储,因此我的Angular 2应用不需要经常访问服务器。 The app currently functions fine when I consume the service directly but when I add my data store layer in between it behaves as though my observable only emits one time (when my navigation bar component reads their currentuser.name). 当我直接使用服务时,该应用程序当前运行良好,但是当我在两者之间添加数据存储层时,它的行为就像我的可观察对象仅发出一次(当我的导航栏组件读取其currentuser.name时)。 Components that consume the service seem to work, and my AuthGuards no longer work (which use the UserProfileStore to get the users info). 使用该服务的组件似乎可以工作,而我的AuthGuard不再起作用(它们使用UserProfileStore获取用户信息)。 I suspect I am using Subject incorrectly and/or need to use some variant of BehaviourSubject or ReplaySubject , but I really don't know where to start. 我怀疑我没有正确使用Subject和/或需要使用BehaviourSubjectReplaySubject的某些变体,但我真的不知道从哪里开始。 My code is semi-based off of this example: Cory Ryan Angular 2 Observable Data Services 我的代码基于该示例是半基础的: Cory Ryan Angular 2 Observable Data Services

Any ideas on what I'm missing? 关于我所缺少的任何想法吗?

user-profile.store.ts user-profile.store.ts

@Injectable()
export class UserProfileStore implements OnDestroy {
private _currentUser:SjfrUser;
private _currentUser$: Subject<MyAppUser>;
private _subscription: Subscription;

constructor(private authService: AuthService) {
    this._currentUser$ = <Subject<MyAppUser>> new Subject();
}

getCurrentUser = () : Observable<MyAppUser> => {

    if (!this._currentUser) {
        let currentUser$ = this.authService.getCurrentUser(); // Performs http
        this._subscription = currentUser$.subscribe((currentUser: MyAppUser) => {                
            this._currentUser = currentUser;
            this._currentUser$.next(this._currentUser);
        });
    }

    return this._currentUser$.asObservable();
}

ngOnDestroy() {
    this._subscription.unsubscribe();
}

} }

Auth-Guard.service.ts Auth-Guard.service.ts

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
    constructor(private userProfileStore: UserProfileStore, private authService: AuthService, private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {        

        let url: string = state.url;

        //var currentUser$ = this.authService.getCurrentUser(); // Works with this
        var currentUser$ = this.userProfileStore.getCurrentUser(); // No longer works            

        return currentUser$.map(x => {
            console.log("AuthGuard" + x.userName);  // This code no longer gets executed when using the datastore
            if (x.isExternalUser && url === '/external') {
                return true;
            } else if (x.isInternalUser && url === '/internal') {
                return true;
            } else {
                this.router.navigate(['/pagenotfound']);
                return false;
            }
        });     


    }
...

Try doing it with .first() after map 尝试在地图后使用.first()进行操作

 @Injectable() export class AuthGuard implements CanActivate, CanActivateChild, CanLoad { constructor(private userProfileStore: UserProfileStore, private authService: AuthService, private router: Router) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { let url: string = state.url; //var currentUser$ = this.authService.getCurrentUser(); // Works with this var currentUser$ = this.userProfileStore.getCurrentUser(); // No longer works return currentUser$.map(x => { console.log("AuthGuard" + x.userName); // This code no longer gets executed when using the datastore if (x.isExternalUser && url === '/external') { return true; } else if (x.isInternalUser && url === '/internal') { return true; } else { this.router.navigate(['/pagenotfound']); return false; } }).first(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM