在vue使用异常处理做错误提示
添加代码
Vue.prototype.errorMsg = function(message, code)
{
alert(message);
// Vue.$vux.toast.show({ 我用vux的,看自己情况自定义错误信息
// text: message,
// type: 'warn'
// })
}
Vue.config.errorHandler = (err, vm, info) => {
if( err instanceof vm.errorMsg) // 不让浏览器显示自定义异常的信息
return true;
}
使用
if( true)
throw new this.errorMsg('请改成false')
alert("这个不会执行了");
为啥要用异常
异常可以停止后面的代码执行,简单多了。
看到好多的项目都是:
if( true){
alert("请改成false")
return;
}
alert("这个不会执行了");
多了个return ;
不嫌麻烦吗? (每次都要写return ;
)
而且还没有能统一管理的入口 。
本作品采用《CC 协议》,转载必须注明作者和本文链接