使用 errorCaptured 鉤子可以捕獲組件內(nèi)部錯誤
使用方法
export default{
data(){},
methods:{},
created(){},
mounted(){},
/**
* 收到三個參數(shù):
* 錯誤對象、發(fā)生錯誤的組件實例
* 以及一個包含錯誤來源信息的字符串。
* 此鉤子可以返回 false 以阻止該錯誤繼續(xù)向上傳播。
*/
errorCaptured(err, vm, info){
console.log(err)
// -> 錯誤返回
console.log(vm)
// -> vue實例
console.log(info)
// -> 在哪個鉤子發(fā)生錯誤
return false
}
}
參數(shù)說明
| 參數(shù) | 說明 | 例子 |
|---|---|---|
| err??? | 錯誤信息 | vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in nextTick: "TypeError: Cannot read property 'getElementsByClassName' of null" |
| vm | vue 實例 | VueComponent {_uid: 7, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} |
| info | 發(fā)生錯誤的鉤子 | created |