duoxuan.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view v-if="question" class="phone-duoxuan-box">
  3. <view class="phone-shiti-question">
  4. <view class="question-num">{{question.onlyNum}}、</view>
  5. <!-- 题干区域 -->
  6. <rich-text :nodes="data.name"></rich-text>
  7. </view>
  8. <!-- 选项区域 -->
  9. <view v-for="(item,index) in data.contents" class="duoxuan-option-box" :class="formatClass(index)" :key="index" @click="onSelect(index)">
  10. <text class="option-change">{{item.number}}</text>
  11. <rich-text :nodes="item.label" class="option-question"></rich-text>
  12. </view>
  13. </view>
  14. </template>
  15. <script setup>
  16. import {
  17. ref,
  18. reactive,
  19. watch
  20. } from 'vue';
  21. import {
  22. useQuestionTools
  23. } from "./useQuestionTools"
  24. const {
  25. getLetterByIndex,
  26. haveSameElements
  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. const emits = defineEmits(['change'])
  42. watch(() => props.question, (val) => formatData(val), {
  43. immediate: true
  44. })
  45. function formatClass(index) {
  46. if (props.showError) {
  47. return {
  48. active_right: props.question.result.some(item => item == index),
  49. showError: !props.question.result.some(item => item == index)
  50. }
  51. } else {
  52. return {
  53. active: props.question.reply.some(item => item == index)
  54. }
  55. }
  56. }
  57. function formatData(val) {
  58. if (val) {
  59. data.name = val.name;
  60. data.contents = val.content.map((item, index) => {
  61. return {
  62. label: item,
  63. number: getLetterByIndex(index)
  64. }
  65. })
  66. }
  67. }
  68. function onSelect(index) {
  69. if (props.showError) {
  70. return;
  71. }
  72. if (props.question.reply) {
  73. if (props.question.reply.some(item => item == index)) {
  74. props.question.reply = props.question.reply.filter(item => item != index);
  75. } else {
  76. props.question.reply.push(index);
  77. }
  78. }
  79. emits('change')
  80. }
  81. </script>