angular - NGRX 通过@Effect 将重点放在输入上
问题描述
我有一个登录组件。
当它加载时,它会从本地存储中获取用户名。
如果存在,我希望将焦点设置在密码字段上。
目前,我正在商店订阅中进行。
@ViewChild('passfield', { static: false }) passfield: ElementRef;
// ...
this.store.select('login')
.subscribe(state => {
this.model.user = state.username;
if (this.model.user) {
// focus on pass field if user is not empty
setTimeout(() => this.passfield.nativeElement.focus(), 0); // <---
}
});
我想将焦点转移到我的组件中的效果和清晰的逻辑上。
- 可以做到吗?
- 我们可以使用 view child 吗?
- 我们想做吗?有更好的方法吗?
解决方案
我会稍微重写一些东西,但绝对要保留组件中的逻辑:
private destroy$ = new Subject<boolean>();
this.store.select('login').pipe(
takeUntil(this.destroy$),
filter(user => user.username),
tap(() => this.passfield.nativeElement.focus())
).subscribe();
ngOnDestroy() {
this.destroy$.next(true);
}
我不认为使用副作用从组件检查商店中的某些内容然后使用同一组件的 DOM 是明智的。可能,但不必要地复杂。
编辑:NGRX 用于状态管理,组件作为状态的纯粹反映。组件应该是选择它如何解释给定状态的组件。
推荐阅读
- sql - SELECT ALL records FROM each TABLE ORDER BY PRIMARY KEYS(EID,CID,FID,EnID) 降序
- amp-html - 带有 VideoObject 的 AMP 页面在 Google Search Console 中返回“所需结构化数据元素中的错误”
- azure-cognitive-search - 用于复杂数据类型的 Azure 搜索索引器
- angular - 我有一个对象数组,但我只想显示前两个,直到它们被删除并且出现下一行
- c - 如何将结构的二进制文件加载到内存中
- python-3.x - GitHub 'Nothing added to Commit' 和 ipynb_checkpoints untracked
- python - 来自回调的猴子补丁对象
- cassandra - Janusgraph 删除顶点并提交完成,但下一个操作仍然看到顶点
- c# - 如何处理“类型''上的警告MVC1004属性与参数'同名”?
- node.js - 请求没有从 serveo.net 主机转发到我的本地主机