备案通过的网站,公司网站如何被收录,番禺人才网入库考试,asp.net+网站开发+实战window.onerror 全局监听所有JS错误#xff0c;包括异步错误但是它是JS级别的#xff0c;识别不了Vue组件信息#xff0c;Vue内部的错误还是用Vue来监听捕捉一些Vue监听不到的错误 errorCaptured生命周期 监听所有下级组件的错误返回false会阻止向上传播到window.onerror … window.onerror 全局监听所有JS错误包括异步错误但是它是JS级别的识别不了Vue组件信息Vue内部的错误还是用Vue来监听捕捉一些Vue监听不到的错误 errorCaptured生命周期 监听所有下级组件的错误返回false会阻止向上传播到window.onerror errorHandler配置 Vue全局错误监听所有组件错误都会汇总到这里但errorCaptured返回false不会传播到这里window.onerror和errorHandler互斥window.onerror不会在被触发这里都是全局错误监听了 异步错误 异步回调里的错误errorHandler监听不到需要使用window.onerror 总结 实际工作中三者结合使用promisepromise没有被catch的报错使用onunhandledrejection监听和setTimeout异步vue里面监听不了 window.addEventListener(unhandledrejection, event {// 捕获 Promise 没有 catch 的错误console.info(unhandledrejection----, event)
})
Promise.reject(错误信息)
// .catch(e console.info(e)) // catch 住了就不会被 unhandledrejection 捕获errorCaptured监听一些重要的、有风险组件的错误window.onerror和errorCaptured候补全局监听
// main.js
const app createApp(App)// 所有组件错误都会汇总到这里
// window.onerror和errorHandler互斥window.onerror不会在被触发这里都是全局错误监听了
// 阻止向window.onerror传播
app.config.errorHandler (error, vm, info) {console.info(errorHandler----, error, vm, info)
}// 在app.vue最上层中监控全局组件
export default {mounted() {/*** msg:错误的信息* source:哪个文件* line:行* column:列* error:错误的对象*/// 可以监听一切js的报错 try...catch 捕获的 error 无法被 window.onerror 监听到window.onerror function (msg, source, line, column, error) {console.info(window.onerror----, msg, source, line, column, error)}// 用addEventListener跟window.onerror效果一样参数不一样// window.addEventListener(error, event {// console.info(window error, event)// })},errorCaptured: (errInfo, vm, info) {console.info(errorCaptured----, errInfo, vm, info)// 返回false会阻止向上传播到window.onerror// 返回false会阻止传播到errorHandler// return false},
}// ErrorDemo.vue
export default {name: ErrorDemo,data() {return {num: 100}},methods: {clickHandler() {try {this.num() // 报错} catch (ex) {console.error(catch....., ex)// try...catch 捕获的 error 无法被 window.onerror 监听到}this.num() // 报错}},mounted() {// 被errorCaptured捕获// throw new Error(mounted 报错)// 异步报错errorHandler、errorCaptured监听不到vue对异步报错监听不了需要使用window.onerror来做// setTimeout(() {// throw new Error(setTimeout 报错)// }, 1000)},
}要统一监听 Vue 组件的报错可以使用 Vue 的全局错误处理机制。Vue 提供了 errorHandler 钩子函数可以在组件发生错误时捕获并处理错误信息。
在你的 Vue 应用的入口文件通常是 main.js中可以使用 Vue.config.errorHandler 方法来设置全局的错误处理函数如下所示
Vue.config.errorHandler function (err, vm, info) {// 在这里可以处理错误比如发送错误日志等console.error(Vue Error:, err);console.error(Component:, vm);console.error(Error Info:, info);
}在这个错误处理函数中err 参数表示捕获到的错误对象vm 参数表示发生错误的 Vue 组件实例info 参数包含有关错误的更多信息比如错误发生的位置等。
通过设置全局的错误处理函数你可以在组件发生错误时捕获错误并进行统一处理比如发送错误日志给服务器、展示错误提示等。
需要注意的是在开发环境下Vue 会默认将错误打印到浏览器的控制台中但在生产环境下默认不会输出错误信息因此建议在生产环境下设置一个错误处理函数来捕获和处理错误。
另外如果你使用的是 Vue 3全局错误处理的设置方式略有不同。你可以使用 app.config.errorHandler 方法来设置全局的错误处理函数具体的代码示例可以参考 Vue 3 的官方文档。 文章转载自: http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.fhbhr.cn.gov.cn.fhbhr.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.dyght.cn.gov.cn.dyght.cn http://www.morning.nzms.cn.gov.cn.nzms.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.czzpm.cn.gov.cn.czzpm.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.pqkgb.cn.gov.cn.pqkgb.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.dwwlg.cn.gov.cn.dwwlg.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn