8.2 KiB
Vuex 错误修复说明
🔍 问题描述
控制台出现多个 Vuex 相关错误:
[vuex] unknown action type: getSysTitle[vuex] unknown getter: isServerRun[vuex] unknown getter: infoMsgUnknown custom element: <asyncModal>
📝 修复内容
1. 修复 getSysTitle action 错误
问题位置:src/components/main/main.vue
错误原因:
- 在
created钩子中调用了this.$store.dispatch('getSysTitle') - 缺少模块命名空间前缀
app/ - 且在
demo-project/src/main.js中已经调用过,不需要重复调用
修改前:
created() {
this.init()
},
methods: {
async init() {
await this.$store.dispatch('getSysTitle') // ❌ 错误
},
}
修改后:
created() {
this.init()
},
methods: {
async init() {
// 获取系统标题(已在 main.js 中调用,这里不需要重复调用)
// await this.$store.dispatch('app/getSysTitle')
},
}
说明:
- ✅ 移除了重复的
getSysTitle调用 - ✅ 系统标题在
demo-project/src/main.js的mounted钩子中统一获取 - ✅ 避免了重复请求
2. 修复 isServerRun getter 错误
问题位置:src/components/main/components/terminal/index.vue
错误原因:
- 使用了不存在的
isServerRungetter - 这是 Terminal 功能相关的状态,但 store 中没有定义
修改前:
computed: {
...mapGetters({
isServerRun: 'isServerRun' // ❌ 不存在的 getter
}),
iconclass() {
let curClass = 'terminal-icon ml10 '
if (this.isServerRun) {
curClass += ' run'
}
return curClass
}
},
修改后:
computed: {
iconclass() {
let curClass = 'terminal-icon ml10'
// Terminal 功能暂时禁用
// if (this.isServerRun) {
// curClass += ' run'
// }
return curClass
}
},
说明:
- ✅ 移除了对不存在的
isServerRungetter 的依赖 - ✅ Terminal 功能暂时禁用
- ✅ 如果需要启用,需要在 store 中添加相应的 state 和 getter
3. 修复 infoMsg getter 错误
问题位置:src/components/main/components/terminal/terminal.vue
错误原因:
- 使用了不存在的
infoMsggetter - 调用了不存在的
clearInfoMsgmutation 和setInteverLogaction
修改前:
computed: {
...mapGetters({
infoMsg: 'infoMsg' // ❌ 不存在的 getter
})
},
methods: {
clearLog() {
this.$store.commit('clearInfoMsg') // ❌ 不存在的 mutation
},
reloadLog() {
this.$store.dispatch('setInteverLog') // ❌ 不存在的 action
},
}
修改后:
computed: {
infoMsg() {
// Terminal 功能暂时禁用
return '终端功能暂未启用'
}
},
methods: {
clearLog() {
// Terminal 功能暂时禁用
this.$Message.info('终端功能暂未启用')
},
reloadLog() {
// Terminal 功能暂时禁用
this.$Message.info('终端功能暂未启用')
},
}
说明:
- ✅ 移除了对不存在的 Vuex 状态的依赖
- ✅ 提供了友好的提示信息
- ✅ 避免了运行时错误
4. 修复 asyncModal 组件未注册错误
问题位置:src/components/main/components/terminal/index.vue
错误原因:
- 模板中使用了
<asyncModal>组件 - 但没有在
components中注册
修改前:
import { mapGetters } from 'vuex'
import Terminal from './terminal.vue'
export default {
components: {
Terminal // ❌ 缺少 asyncModal
},
}
修改后:
import Terminal from './terminal.vue'
import asyncModal from '@/components/asyncModal'
export default {
components: {
Terminal,
asyncModal // ✅ 添加 asyncModal 组件
},
}
说明:
- ✅ 导入了
asyncModal组件 - ✅ 在
components中注册 - ✅ 移除了不需要的
mapGetters导入
📊 修复总结
修改的文件
-
src/components/main/main.vue- 移除了重复的
getSysTitle调用
- 移除了重复的
-
src/components/main/components/terminal/index.vue- 移除了
isServerRungetter 的使用 - 添加了
asyncModal组件注册
- 移除了
-
src/components/main/components/terminal/terminal.vue- 移除了
infoMsggetter 的使用 - 修改了
clearLog和reloadLog方法
- 移除了
错误修复
- ✅
[vuex] unknown action type: getSysTitle- 已修复 - ✅
[vuex] unknown getter: isServerRun- 已修复 - ✅
[vuex] unknown getter: infoMsg- 已修复 - ✅
Unknown custom element: <asyncModal>- 已修复
💡 Terminal 功能说明
当前状态
Terminal 功能已暂时禁用,因为:
- 缺少相关的 Vuex store 配置
- 缺少后端接口支持
- 不影响系统核心功能
如何启用 Terminal 功能
如果需要启用 Terminal 功能,需要:
1. 在 Vuex store 中添加 Terminal 模块
创建 src/store/terminal.js:
export default {
namespaced: true,
state: {
isServerRun: false,
infoMsg: ''
},
getters: {
isServerRun: state => state.isServerRun,
infoMsg: state => state.infoMsg
},
mutations: {
setServerRun(state, value) {
state.isServerRun = value
},
setInfoMsg(state, msg) {
state.infoMsg += msg + '\n'
},
clearInfoMsg(state) {
state.infoMsg = ''
}
},
actions: {
async setInteverLog({ commit }) {
// 实现日志加载逻辑
}
}
}
2. 在 src/store/index.js 中注册模块
import terminal from './terminal'
export default new Vuex.Store({
modules: {
user,
app,
terminal // 添加 terminal 模块
}
})
3. 更新组件代码
src/components/main/components/terminal/index.vue:
computed: {
...mapGetters('terminal', {
isServerRun: 'isServerRun'
}),
iconclass() {
let curClass = 'terminal-icon ml10 '
if (this.isServerRun) {
curClass += ' run'
}
return curClass
}
}
src/components/main/components/terminal/terminal.vue:
computed: {
...mapGetters('terminal', {
infoMsg: 'infoMsg'
})
},
methods: {
clearLog() {
this.$store.commit('terminal/clearInfoMsg')
},
reloadLog() {
this.$store.dispatch('terminal/setInteverLog')
}
}
⚠️ 注意事项
1. 系统标题获取时机
系统标题在 demo-project/src/main.js 的 mounted 钩子中获取:
mounted() {
AdminFramework.uiTool.setRem()
// 只在已登录时获取系统标题
const token = this.$store.state.user.token
if (token) {
this.$store.dispatch('app/getSysTitle', {
defaultTitle: 'Demo 管理系统',
defaultLogo: ''
})
} else {
document.title = 'Demo 管理系统'
}
}
不要在其他地方重复调用,避免:
- 重复的 HTTP 请求
- 不必要的性能开销
- 可能的竞态条件
2. Vuex 模块命名空间
所有 Vuex 模块都使用了 namespaced: true:
// ✅ 正确的调用方式
this.$store.dispatch('app/getSysTitle')
this.$store.commit('user/setToken', token)
this.$store.getters['user/userName']
// ❌ 错误的调用方式
this.$store.dispatch('getSysTitle')
this.$store.commit('setToken', token)
this.$store.getters['userName']
使用 mapGetters/mapActions 时:
// ✅ 正确
...mapGetters('app', ['sysFormModel'])
...mapActions('user', ['handleLogin'])
// 或者
...mapGetters({
sysFormModel: 'app/sysFormModel'
})
3. 组件注册
使用第三方组件或自定义组件时,必须:
- 导入组件
- 在
components中注册 - 才能在模板中使用
// ✅ 正确
import asyncModal from '@/components/asyncModal'
export default {
components: {
asyncModal
}
}
// ❌ 错误(直接在模板中使用未注册的组件)
<template>
<asyncModal></asyncModal>
</template>
✅ 验证清单
功能验证
- ✅ 登录功能正常
- ✅ 系统标题正确显示
- ✅ 菜单正确显示
- ✅ 页面跳转正常
- ✅ 控制台没有 Vuex 错误
错误检查
- ✅ 没有
[vuex] unknown action错误 - ✅ 没有
[vuex] unknown getter错误 - ✅ 没有
Unknown custom element错误 - ✅ 没有组件注册错误
所有 Vuex 错误已修复! 🎉
现在系统可以正常运行,Terminal 功能暂时禁用但不影响核心功能。