错误句柄 angular 7
errorHandle angular 7
如何在这种情况下管理错误块 - 在服务中管理错误并发送到组件
这里在第一次加载时将数据存储在BehaviorSubject中,并从我需要的地方订阅数据。
只是在应用程序运行时从 app.component 初始化预加载函数 - 但我想知道它是否出错
//**app component**
//just initializing preload function() when the app runs - but I want to know here if its an error
ngOnInit() {
this.projectsService.preloadAllProjects();
}
// == services ==
//preload all projects
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe( map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
});
};
如果您想遵循主题路径:
//**app component**
//just initializing preload function() when the app runs - but I want to know here if its an error
ngOnInit() {
this.projectsService.preloadAllProjects();
this.projectService.error$.subscribe(error => console.log(error));
}
// == services ==
//preload all projects
error$: Subject<string> = new Subject();
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe( map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
}, error => this.error$.next(error);
};
或者你基本上可以 return 来自 preloadAllProjects
的 Observable:
//**app component**
//just initializing preload function() when the app runs - but I want to know here if its an error
ngOnInit() {
this.projectsService.preloadAllProjects().subscribe(success => {}, error => console.log(error))
}
// == services ==
//preload all projects
error$: Subject<string> = new Subject();
preloadAllProjects(){
return new Observable(observer => {
this.http.get('/api/project/allprojects').pipe( map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
observer.next(true);
observer.complete();
}, error => observer.error(error);
});
};
您可以添加 catchError 运算符,或将第二个函数传递给 subscribe 函数。
解决方案 1.:
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe(
catchError(err => console.error(err)),
map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
});
};
解决方案 2.:
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe(
map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
},
error => console.error(error)
);
};
如何在这种情况下管理错误块 - 在服务中管理错误并发送到组件
这里在第一次加载时将数据存储在BehaviorSubject中,并从我需要的地方订阅数据。
只是在应用程序运行时从 app.component 初始化预加载函数 - 但我想知道它是否出错
//**app component**
//just initializing preload function() when the app runs - but I want to know here if its an error
ngOnInit() {
this.projectsService.preloadAllProjects();
}
// == services ==
//preload all projects
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe( map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
});
};
如果您想遵循主题路径:
//**app component**
//just initializing preload function() when the app runs - but I want to know here if its an error
ngOnInit() {
this.projectsService.preloadAllProjects();
this.projectService.error$.subscribe(error => console.log(error));
}
// == services ==
//preload all projects
error$: Subject<string> = new Subject();
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe( map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
}, error => this.error$.next(error);
};
或者你基本上可以 return 来自 preloadAllProjects
的 Observable:
//**app component**
//just initializing preload function() when the app runs - but I want to know here if its an error
ngOnInit() {
this.projectsService.preloadAllProjects().subscribe(success => {}, error => console.log(error))
}
// == services ==
//preload all projects
error$: Subject<string> = new Subject();
preloadAllProjects(){
return new Observable(observer => {
this.http.get('/api/project/allprojects').pipe( map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
observer.next(true);
observer.complete();
}, error => observer.error(error);
});
};
您可以添加 catchError 运算符,或将第二个函数传递给 subscribe 函数。
解决方案 1.:
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe(
catchError(err => console.error(err)),
map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
});
};
解决方案 2.:
preloadAllProjects(){
this.http.get('/api/project/allprojects').pipe(
map( response => response )).subscribe(result => {
this.allProjectsBehavior.next(result);
},
error => console.error(error)
);
};