vue如何禁止打開調試模式方法
vue禁止打開調試模式
//在app.vue添加一下代碼
<template>
<div id="app">
<router-view></router-view>
</div>
</template><script>
export default {
name: "App",
data() {
return {};
},
methods:{
},
mounted() {
if (process.env.mode === 'production') {
(function noDebugger() {
function testDebugger() {
var d = new Date();
debugger;
if (new Date() - d > 10) {
document.body.innerHTML = '<div>管理員已禁用調試模式!!!年輕人,不要太好奇</div>';
alert("管理員已禁用調試模式")
return true;
}
return false;
}
function start() {
while (testDebugger()) {
testDebugger();
}
}
if (!testDebugger()) {
window.onblur = function () {
setTimeout(function () {
start();
}, 500);
};
} else {
start();
}
})();
}
},
};
</script><style>
#app {
/* font-family: Avenir, Helvetica, Arial, sans-serif; */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
/* width: 100vw; */
height: 100vh;
overflow: hidden;
box-sizing: border-box;
}
</style>vue提供的三種調試方式
一、在 VS Code 中配置調試
使用 Vue CLI 2搭建項目時:
更新 config/index.js 內的 devtool property:
devtool: 'source-map',
點擊在 Activity Bar 里的 Debugger 圖標來到 Debug 視圖:

選擇 Chrome/Firefox:Launch 環(huán)境。
將 launch.json 的內容替換為:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "vuejs: chrome",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/src",
"breakOnLoad": true,
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
},
{
"type": "firefox",
"request": "launch",
"name": "vuejs: firefox",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/src",
"pathMappings": [{ "url": "webpack:///src/", "path": "${webRoot}/" }]
}
]
}
開始調試:
設置斷點:

#啟動項目npm run dev
在debug頁面選擇“vuejs:chrome”:

二、debugger語句
推薦?。。。。。。?!
function potentiallyBuggyCode() {
debugger
// do potentially buggy stuff to examine, step through, etc.
}
瀏覽器:F12打開DevTools,當運行程序后,會停在debbger語句:

注意:
當安裝了Eslint插件時,點擊快速修復,Disable no-debugger for this line.
不然,保存時會自動清除debugger語句。

三、Vue Devtools
谷歌瀏覽器的插件。
詳情參考官方鏈接:https://cn.vuejs.org/v2/cookbook/debugging-in-vscode.html#Vue-Devtools
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue加載動畫element ui V-loading屬性的踩坑記錄
這篇文章主要介紹了vue加載動畫element ui V-loading屬性方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

