Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vue] 怎么捕获组件vue的错误信息? #327

Open
haizhilin2013 opened this issue Jun 20, 2019 · 5 comments
Open

[vue] 怎么捕获组件vue的错误信息? #327

haizhilin2013 opened this issue Jun 20, 2019 · 5 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] 怎么捕获组件vue的错误信息?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 20, 2019
@Sihan-Tan
Copy link

errorCaptured

@zhaofeipeter
Copy link

errorHandler

@jiefancis
Copy link

Vue.config.errorHandler = function (err, vm, info) {} 捕获所有组件
errorCaptured
捕获当前组件的后代组件的错误

@sc950828
Copy link

全局: errorhandler
组件内:errorCaptured

@Cai-zhiji
Copy link

全局错误处理

可以通过Vue的config.errorHandler方法来注册全局错误处理器,用于捕获整个应用程序范围内的未捕获的错误。
在全局错误处理器中,你可以记录错误信息、发送错误报告或执行其他自定义的错误处理逻辑。

Vue.config.errorHandler = function(err, vm, info) {
  // 处理错误,例如发送错误报告
  console.error('Global Error:', err, vm, info);
};

组件错误边界

Vue 2.x并没有内置的组件级错误处理机制,但你可以使用Vue的errorCaptured钩子函数来捕获并处理单个组件内的错误。
在组件中定义errorCaptured钩子函数,用于捕获该组件及其子组件中发生的错误。
在errorCaptured钩子函数中,你可以对错误进行处理、展示错误信息或展示备用的UI内容。

Vue.component('my-component', {
  // ...
  errorCaptured: function(err, vm, info) {
    // 处理错误,例如展示错误信息
    console.error('Component Error:', err, vm, info);
    // 返回 `false` 以阻止错误继续向上冒泡
    return false;
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

6 participants