danxuan.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <view v-if="question">
  3. <!-- 标题区域 -->
  4. <view>单选题:</view>
  5. <!-- 题干区域 -->
  6. <view v-html="data.name"></view>
  7. <!-- 选项区域 -->
  8. <view>
  9. <view v-for="(item,index) in data.contents" :class="formatClass(index)" :key="index" @click="onSelect(index)">
  10. <text>{{item.number}}</text>
  11. <text>{{item.label}}</text>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. ref,
  19. reactive,
  20. watch
  21. } from 'vue';
  22. import {
  23. useQuestionTools
  24. } from "./useQuestionTools"
  25. const {
  26. getLetterByIndex
  27. } = useQuestionTools();
  28. const props = defineProps({
  29. question: {
  30. type: Object,
  31. },
  32. showError: {
  33. type: Boolean,
  34. default: false
  35. }
  36. })
  37. const data = reactive({
  38. name: '', //题干数据
  39. contents: [], // 选项数据
  40. })
  41. watch(() => props.question, (val) => formatData(val), {
  42. immediate: true
  43. })
  44. function formatClass(index) {
  45. if (props.showError) {
  46. return {
  47. active_right: props.question.result == index,
  48. showError: props.question.reply == index && props.question.result != index
  49. }
  50. } else {
  51. return {
  52. active: props.question.reply == index
  53. }
  54. }
  55. }
  56. function formatData(val) {
  57. if (val) {
  58. data.name = val.name;
  59. data.contents = val.optList.map((item, index) => {
  60. return {
  61. label: item,
  62. number: getLetterByIndex(index)
  63. }
  64. })
  65. }
  66. }
  67. function onSelect(index) {
  68. if (props.showError) {
  69. return;
  70. }
  71. props.question.reply = index;
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. .active {
  76. background-color: blue; // 单选题选中的颜色
  77. }
  78. .showError {
  79. background-color: red; // 答案解析:单选错误选中颜色
  80. }
  81. .active_right {
  82. background-color: green; // 答案解析:单选正确颜色
  83. }
  84. </style>