Просмотр исходного кода

refactor(question): 简化组件路径引用方式

将硬编码的组件路径改为使用组件名称映射,移除动态导入改用静态导入
优化组件加载方式,提升代码可维护性
tanshanming 7 месяцев назад
Родитель
Сommit
a3a1255fc3
2 измененных файлов с 22 добавлено и 10 удалено
  1. 5 5
      src/store/exam.js
  2. 17 5
      src/views/exm/question/index.vue

+ 5 - 5
src/store/exam.js

@@ -94,11 +94,11 @@ export const useExamStore = defineStore('exam', {
 					{ key: 5, value: '简答题' }
 				],
 				editUrlEnum: [
-					{ key: 1, value: '/src/views/exm/question/edit/single-choice.vue', name: '单选题' },
-					{ key: 2, value: '/src/views/exm/question/edit/multiple-choice.vue', name: '多选题' },
-					{ key: 3, value: '/src/views/exm/question/edit/true-false.vue', name: '判断题' },
-					{ key: 4, value: '/src/views/exm/question/edit/gap-filling.vue', name: '填空题' },
-					{ key: 5, value: '/src/views/exm/question/edit/short-answer.vue', name: '简答题' }
+					{ key: 1, value: 'single-choice', name: '单选题' },
+					{ key: 2, value: 'multiple-choice', name: '多选题' },
+					{ key: 3, value: 'true-false', name: '判断题' },
+					{ key: 4, value: 'gap-filling', name: '填空题' },
+					{ key: 5, value: 'short-answer', name: '简答题' }
 				],
 				answer: {
 					doRightTag: [

+ 17 - 5
src/views/exm/question/index.vue

@@ -153,7 +153,12 @@
 </template>
 
 <script setup>
-	import { ref, reactive, computed, onMounted, defineAsyncComponent, shallowRef } from 'vue'
+	import { ref, reactive, computed, onMounted, shallowRef } from 'vue'
+	import SingleChoice from './edit/single-choice.vue'
+	import MultipleChoice from './edit/multiple-choice.vue'
+	import TrueFalse from './edit/true-false.vue'
+	import GapFilling from './edit/gap-filling.vue'
+	import ShortAnswer from './edit/short-answer.vue'
 	import { useExamStore } from '@/store/exam'
 	import tQuestionApi from '@/api/exam/question/tQuestionApi'
 	import customPagination from '@/components/customPagination.vue'
@@ -194,9 +199,16 @@
 	// 动态抽屉相关
 	const drawerOpen = ref(false)
 	const currentComponent = shallowRef(null)
-	const openDrawer = (componentPath) => {
+	const componentMap = {
+		'single-choice': SingleChoice,
+		'multiple-choice': MultipleChoice,
+		'true-false': TrueFalse,
+		'gap-filling': GapFilling,
+		'short-answer': ShortAnswer
+	}
+	const openDrawer = (componentName) => {
 		currentComponentId.value = 0
-		currentComponent.value = defineAsyncComponent(() => import(/* @vite-ignore */ componentPath))
+		currentComponent.value = componentMap[componentName]
 		drawerOpen.value = true
 	}
 	const closeDrawer = () => {
@@ -262,8 +274,8 @@
 	}
 
 	const editQuestion = (row) => {
-		const url = enumFormat(editUrlEnum.value, row.questionType)
-		currentComponent.value = defineAsyncComponent(() => import(/* @vite-ignore */ url))
+		const componentName = enumFormat(editUrlEnum.value, row.questionType)
+		currentComponent.value = componentMap[componentName]
 		drawerOpen.value = true
 		currentComponentId.value = row.id
 	}