我有一个吐司经理,试着接住盖子,它能像我所期望的那样工作。
showToast(status: string, message: string) {
try {
if (status === 'success') {
this.toastr.showSuccessToast(message)
}
else if (status === 'warning'){
this.toastr.showWarningToast(message)
}
else {
this.toastr.showErrorToast(message)
}
} catch {
console.log('Toast failed');
}在我看来,这可能是无效的,但我不知道我是否应该删除试捕或如果条件
发布于 2021-03-30 07:23:24
我想在这里做两个改变:
switch而不是if else。易于阅读和维护。try catch块。这个块可以捕获的唯一例外情况可以由showSuccessToast、showWarningToast和toastr的showErrorToast引发。我认为toastr是由ng引导程序提供的服务。如果您不信任这样的第三方服务,则需要在许多地方捕获错误。相反,我会考虑避免使用try catch通过在全球一级处理此类异常污染代码。https://stackoverflow.com/questions/66856059
复制相似问题