Vuejs,在生产模式下捕获警告

Ale*_*sky 2 vue.js vuejs2

我们的应用程序使用 magento 的模板,有时会触发错误(例如错误的 props 或特殊字符)。这些错误将在生产模式下被消除。

但我们应该通过 TrackJs 来跟踪它。那么,是否可以在生产模式下捕获警告?

警告示例:

[Vue warn]:编译模板时出错:

Fer*_*big 6

当 Vue 在生产模式下运行时,所有错误都隐藏在 bij 设计中,这不是可以更改的,除非运行开发版本。这背后的原因是,在生产内部,您最好使用构建工具或自定义后端预编译所有模板。

在开发中,您可以在 Vue 配置中使用 anerrorHandler和 a :warnHandler

Vue.config.errorHandler = function (err, vm, info) {
    // handle error
    // `info` is a Vue-specific error info, e.g. which lifecycle hook
    // the error was found in. Only available in 2.2.0+
    console.log('Custom vue error handler: ', err, vm.name, info);
};
Vue.config.warnHandler = function (err, vm, info) {
    // handle error
    // `info` is a Vue-specific error info, e.g. which lifecycle hook
    // the error was found in. Only available in 2.2.0+
    console.log('Custom vue warn handler: ', err, vm.name, info);
};

// Prevent vue from spamming the console with "helpful" tips
Vue.config.productionTip = false;

var app = new Vue({
    el: '#app',
    name: 'main',
    template: '<div></div><div></div>',
    data: {
        message: 'Hello Vue!'
    },
});
Run Code Online (Sandbox Code Playgroud)
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<div id="app">
</div>
Run Code Online (Sandbox Code Playgroud)

对于模板问题,Vue 使用 warn 处理程序,但对于方法抛出的错误,它使用 errorhandler