Skip to content

Commit

Permalink
feat: 新增error展示模块
Browse files Browse the repository at this point in the history
  • Loading branch information
simply-none committed Jan 19, 2024
1 parent b10e42b commit d0ad3f9
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<script setup></script>
<script setup>
import ErrorDialog from './components/errorDialog.vue';
</script>

<template>
<RouterView />
<ErrorDialog/>
</template>

<style scoped></style>
145 changes: 145 additions & 0 deletions src/components/errorDialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<template>
<div class="eD-outer">
<el-dialog v-model="visible" :show-close="false" class="eD" :class="{ 'is-pc': isPC() }" :append-to-body="false">
<template #header="{ close, titleId, titleClass }">
<div class="eD-header" :class="titleClass">

</div>
</template>
<div class="eD-header">
{{ (errorList || [])?.length }}条错误信息
</div>

<div class="eD-error">

<p class="eD-error-item" v-for="(error,index) in errorList" :key="error.id">
<div class="eD-error-title">{{index + 1}}.{{ error?.msg }}</div>
<div class="eD-error-stack">{{ error?.stack }}</div>
</p>
</div>
<template #footer>
<span class="eD-footer">
<el-button @click="copyErrorInfo">复制</el-button>
<el-button type="primary" @click="toggleVisible(false)">
关闭
</el-button>
</span>
</template>
</el-dialog>
</div>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import { ElButton, ElDialog } from "element-plus";
import { CircleCloseFilled } from "@element-plus/icons-vue";
import { storeToRefs } from 'pinia'
import { useErrorStore } from "../stores/error";
import { isPC } from "../utils/common";
console.log(isPC(), 'isPCccc')
let useError = useErrorStore()
let { errorList } = storeToRefs(useError)
let { clearError } = useError
let visible = computed(() => !!errorList.value)
watch(errorList, (n, o) => {
console.log(n, '当前的错误')
if (n) {
n.forEach(e => {
console.log(e)
})
}
})
function copyErrorInfo() {
console.log('复制成功')
}
function toggleVisible() {
clearError()
}
</script>

<style scoped lang="scss">
.eD-outer {
:deep(.el-overlay-dialog) {
overflow: hidden;
}
}
:deep(.eD) {
top: 0;
bottom: 0;
margin: auto 10%;
position: absolute;
width: 80%;
height: 80%;
overflow: hidden;
&.is-pc {
margin: auto calc((100% - 760px) / 2);
width: 760px;
}
@media (max-width: 960px) {
margin: auto 10% !important;
width: 80% !important;
}
.el-dialog__header {
padding: 0;
}
.el-dialog__body {
width: 100%;
height: calc(100% - 72px);
overflow: hidden;
padding: 20px 20px 0 20px;
margin-bottom: 20px;
}
.el-dialog__footer {
text-align: center;
padding-top: 0;
}
}
.eD {
margin: 0 auto;
height: 80%;
top: 10%;
bottom: 10%;
padding: 0;
color: red;
&-header {
margin-bottom: 10px;
text-align: center;
font-size: 1.5em;
font-weight: 900;
}
}
.eD-error {
height: calc(100% - 42px);
overflow: auto;
&-item {
&+& {
margin-top: 10px;
padding-top: 10px;
border-top: 3px solid #ebebeb;
}
}
&-title {
font-weight: 900;
}
&-stack {
word-wrap: break-word;
}
}
</style>
20 changes: 15 additions & 5 deletions src/stores/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { defineStore, storeToRefs } from "pinia";

// 本store,用处在于获取、设置基础信息数据
export const useErrorStore = defineStore("error", () => {
let errorList = ref([]);
let errorList = ref();
let errorListCache = ref([]);

watch(() => errorList.value, (n, o) => {
Expand All @@ -21,13 +21,23 @@ export const useErrorStore = defineStore("error", () => {
})

function addError(err) {
console.log('hhhhh')
errorList.value = errorList.value.concat(err);
errorListCache.value = errorList.value.concat(err);
// 防止卡死,限制数量
if (errorList.value && errorList.value.length >= 50) {
return false
}
err = {
msg: err.message,
stack: err.stack.toString()

}
errorList.value = (errorList.value || []).concat(err);
errorListCache.value = errorListCache.value.concat(err);
}

function clearError() {
errorList.value = [];
errorList.value = null;
console.log(errorList.value)

}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/stores/words.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const useWordStore = defineStore("word", () => {

watch(willStudyWords, (n, o) => {
if (n) {
console.log(willStudyWords.value, 'hhhhhhhhh')
console.log(willStudyWords.value?.length, 'hhhhhhhhh')
stopInitWatch && stopInitWatch();
isStopInitWatch.value = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function isPC() {
let mobile = ['iphone', 'android', 'harmony', 'mobile']
let userAgent = window.navigator.userAgent.toLocaleLowerCase()

let isMobile = mobile.some(agent => userAgent.includes(mobile))
let isMobile = mobile.some(agent => userAgent.includes(agent))

if (isMobile) {
return false
Expand Down

0 comments on commit d0ad3f9

Please sign in to comment.