duoxuan.vue 1.9 KB

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