【发布时间】:2020-08-15 12:57:23
【问题描述】:
我的应用程序中有两个页面Dashboard 和Transactions 以及一个控制导航的Sidebar 组件。
当我第一次从Dashboard 导航到Transactions 时,我订阅了商店中的用户状态。一切都按预期工作,我获取了用户 ID 并将其注入到子组件中以使用。
export class TransactionsComponent implements OnInit {
userID;
constructor(private store: Store<AppState>, private spinner: NgxSpinnerService) {}
ngOnInit() {
this.spinner.show();
this.store.select('user').subscribe(
user => { if (user) { this.userID = user.uid; } }
);
}
}
当我导航回Dashboard 页面,然后返回Transactions 页面时,我遇到了用户ID 永远无法解析和无休止的加载微调器。
在我的Sidebar 中,从交易页面导航到仪表板时,此订阅会消失。
export class SidebarComponent implements OnInit {
user$: Observable<User>;
constructor(public auth: AuthService, private store: Store<AppState>) {}
ngOnInit(): void {
this.user$ = this.store.select('user');
this.store.dispatch(new userActions.GetUser());
}
}
这是我将检索用户 ID 的效果
@Effect()
getUser: Observable<Action> = this.actions.pipe(
ofType(userActions.GET_USER),
map((action: userActions.GetUser) => action.payload ),
switchMap(payload => this.afAuth.authState),
delay(2000), // delay to show loading spinner can be deleted
map( authData => {
if (authData) {
// User logged in
const user = new User(authData.uid, authData.displayName);
return new userActions.Authenticated(user);
} else {
return new userActions.NotAuthenticated();
}
}),
catchError(err => of(new userActions.AuthError()) )
);
我希望Transactions 组件能够保持用户 ID 的状态,并且在我返回时不需要加载微调器。
【问题讨论】:
-
您实现了一个全局状态解决方案,因此您的组件不必记住用户 ID。这是商店的任务。如果您没有在其他任何地方覆盖商店中的用户,您应该在您的问题中发布更多代码,因为您粘贴在那里的内容对我来说看起来很正常。你认为有可能在stackblitz.com 上建立最小复制吗?
-
好的,我在从交易页面导航到仪表板页面时订阅消失的地方添加了更多代码。我还添加了名为 GetUser 的效果来检索用户。这可能是问题的一部分吗?
-
不..没什么奇怪的......也许在减速器逻辑中?此外,在组件逻辑中,您将在 OnInit 挂钩上启动微调器。你在哪里阻止它? Stackblitz 演示对您有帮助 ;)
-
在
TransactionsComponent中,我认为您需要在ngOnInit()中调度您的一项操作以获取用户this.store.dispatch(YourUserActions.Load())
标签: angular typescript angular-routing ngrx