🐞 fix: 错误修复和代码健壮性

- 在 KnowledgeBase.vue 中修正无效的 v-class 指令为 class 属性的问题
- 在 ConsoleDisplayer.vue 中修正 historyNum 属性类型不匹配的问题
- 解决控制台中的 Vue 警告信息
- 在访问 status 前对 err.response 进行空值检查
- 防止“无法读取未定义对象的属性”错误
- 提高 catch 块中错误处理的健壮性
- 对 API 响应数据进行空值检查
- 在处理之前确保数组类型验证
- 修复“无法读取 null 对象的属性”错误
- 改进 beforeUnmount 生命周期中的 D3.js 清理工作
- 对图形数据处理添加防御性编程
This commit is contained in:
IGCrystal
2025-06-15 22:45:28 +08:00
parent c5f9b5861f
commit 075b008740
4 changed files with 70 additions and 14 deletions
@@ -51,7 +51,7 @@ export default {
props: {
historyNum: {
type: String,
default: -1
default: "-1"
},
showLevelBtns: {
type: Boolean,
+13 -2
View File
@@ -45,10 +45,21 @@ export default {
},
methods: {
navigateTo(tab) {
this.$router.push(`/alkaid/${tab}`);
try {
if (this.$router && typeof this.$router.push === 'function') {
this.$router.push(`/alkaid/${tab}`);
}
} catch (error) {
console.warn('Navigation error:', error);
}
},
isActive(tab) {
return this.$route.path.includes(`/alkaid/${tab}`);
try {
return this.$route && this.$route.path.includes(`/alkaid/${tab}`);
} catch (error) {
console.warn('Route check error:', error);
return false;
}
}
},
mounted() {
+2 -2
View File
@@ -5,7 +5,7 @@
<div v-if="!installed" class="d-flex align-center justify-center flex-column"
style="flex-grow: 1; width: 100%; height: 100%;">
<h2>还没有安装知识库插件
<v-icon v-class="ml - 2" size="small" color="grey"
<v-icon class="ml-2" size="small" color="grey"
@click="openUrl('https://astrbot.app/use/knowledge-base.html')">mdi-information-outline</v-icon>
</h2>
<v-btn style="margin-top: 16px;" variant="tonal" color="primary" @click="installPlugin"
@@ -23,7 +23,7 @@
</div>
<div v-else>
<h2 class="mb-4">知识库列表
<v-icon v-class="ml - 2" size="x-small" color="grey"
<v-icon class="ml-2" size="x-small" color="grey"
@click="openUrl('https://astrbot.app/use/knowledge-base.html')">mdi-information-outline</v-icon>
</h2>
<v-btn class="mb-4" prepend-icon="mdi-plus" variant="tonal" color="primary"
+54 -9
View File
@@ -310,9 +310,25 @@ export default {
this.ltmGetUserIds();
},
beforeUnmount() {
// 停止D3仿真
if (this.simulation) {
this.simulation.stop();
}
// 清理DOM元素
if (this.svg) {
try {
this.svg.remove();
} catch (e) {
console.warn('Error removing SVG:', e);
}
}
// 重置数据
this.nodes = [];
this.links = [];
this.userIdList = [];
this.searchResults = [];
},
methods: {
// 添加搜索记忆方法
@@ -410,8 +426,10 @@ export default {
axios.get('/api/plug/alkaid/ltm/graph', { params })
.then(response => {
let nodesRaw = response.data.data.nodes;
let edgesRaw = response.data.data.edges;
const data = response.data.data || {};
// 确保数据是数组类型,并且先检查data是否存在
let nodesRaw = data && Array.isArray(data.nodes) ? data.nodes : [];
let edgesRaw = data && Array.isArray(data.edges) ? data.edges : [];
this.node_data = nodesRaw;
this.edge_data = edgesRaw;
@@ -453,6 +471,11 @@ export default {
})
.catch(error => {
console.error('Error fetching graph data:', error);
// 出错时重置为空数组
this.nodes = [];
this.links = [];
this.node_data = [];
this.edge_data = [];
})
.finally(() => {
this.isLoading = false;
@@ -462,10 +485,13 @@ export default {
ltmGetUserIds() {
axios.get('/api/plug/alkaid/ltm/user_ids')
.then(response => {
this.userIdList = response.data.data;
// 确保返回的数据是数组类型
const data = response.data.data;
this.userIdList = Array.isArray(data) ? data : [];
})
.catch(error => {
console.error('Error fetching user IDs:', error);
this.userIdList = []; // 出错时设置为空数组
});
},
@@ -575,10 +601,20 @@ export default {
initD3Graph() {
const container = document.getElementById("graph-container");
if (!container) return;
d3.select("#graph-container svg").remove();
const width = container.clientWidth;
const height = container.clientHeight;
if (!container) {
console.warn('Graph container not found');
return;
}
// 安全清理现有SVG
try {
d3.select("#graph-container svg").remove();
} catch (e) {
console.warn('Error removing existing SVG:', e);
}
const width = container.clientWidth || 800;
const height = container.clientHeight || 600;
const svg = d3.select("#graph-container")
.append("svg")
.attr("width", "100%")
@@ -608,9 +644,18 @@ export default {
},
updateD3Graph() {
if (!this.svg || !this.simulation) return;
if (!this.svg || !this.simulation || !this.g) {
console.warn('D3 elements not ready for update');
return;
}
const g = this.g;
g.selectAll("*").remove();
try {
g.selectAll("*").remove();
} catch (e) {
console.warn('Error clearing D3 graph:', e);
return;
}
// 添加箭头定义
g.append("defs").append("marker")