Angular中ViewChild\ngAfterViewInit\Promise的使用,在父组件初始化时等待子组件的返回值
2024-08-30 02:59:09
1、子component中的异步方法
initCreateJob = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
this.spiderFormService.saveUserJob(this.spiderJobInfo).subscribe((res: Res) => {
if (res.code === 1) {
// val = res.data;
resolve(res.data)
// fn(this.spiderJobInfo.jobInfoId)
}else{
// 提交异常
reject("Error code: " + JSON.stringify(res))
}
});
}, 1000)
}
);
2、加载多个子组件
引入组件
<app-spider-form #test1></app-spider-form>
<app-image-check-form #test2></app-image-check-form>
3、父组件的component中引用子组件component
export class parentComponent implements OnInit,AfterViewInit{
// 引用子组件
@ViewChild('test1', {static: false}) test1;
@ViewChild('test2', {static: false}) test2;
constructor() {
}
ngOnInit() {
}
ngAfterViewInit(){
// 子组件加载完毕触发
this.initSpiderJob();
}
initSpiderJob(){
forkJoin([
from(this.test1.initCreateJob()).pipe(catchError(error => of(`Bad Promise: ${error}`))),
from(this.test2.initCreateJob()).pipe(catchError(error => of(error)))
])
.subscribe(results => {
console.log("results" + JSON.stringify(results))
});
}
}
个人博客 蜗牛
最新文章
- 添加AppWidget功能
- C语言正则表达式详解 regcomp() regexec() regfree()详解
- rspec中的shared_examples与shared_context有什么不同
- PHP面向对象07_PDO
- git打tag 三步骤
- 外部按键 控制 LED 中断 (参考 http://www.oschina.net/question/565065_115196?sort=time )
- ASP.NET-【状态管理】-Cookie小结
- python匿名函数(lambda)
- DataGridView 的cell赋值没有线程间访问的限制吗?
- DataGuard相同SID物理Standby搭建
- Objective-c setObject:forKey:和setValue:forKey:的区别
- iOS 推荐博客
- 天天模拟器极速畅玩靠谱游戏《仙境传说RO:复兴》
- C++中怎样获取类成员的指针
- Python软件目录结构规范
- 2018-2019-2 网络对抗技术 20165319 Exp3 免杀原理与实践
- Linux&#160;学习笔记之超详细基础linux命令&#160;Part&#160;10
- [No0000F2]ip安全监视器
- shlve 模块
- OpenGL ES3 非常好的系列文章
热门文章
- 二叉树 B-树B+树
- Spring Cloud Feign 调用过程分析
- 动态查找之二叉树查找 c++实现
- Python示例项目学习
- Attribute application@allowBackup value=(true) from AndroidManifest.xml:7:9-35
- tensorflow keras analysis
- 运维笔记--给正在运行的Docker容器动态绑定卷组(挂载指定目录)
- macOS 10.15 Catalina xxx.app已损坏,无法打开,你应该将它移到废纸篓解决方法
- 浅入深出ETCD之【raft原理】
- python初级(302) 6 对象(一)