danxuan.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view v-if="question" class="phone-danxuan-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="danxuan-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. } = useQuestionTools();
  27. const props = defineProps({
  28. question: {
  29. type: Object,
  30. },
  31. showError: {
  32. type: Boolean,
  33. default: false
  34. }
  35. })
  36. const emits = defineEmits(['change'])
  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. if (props.question.reply === ''||props.question.reply === null ) {
  52. return {
  53. active: false
  54. }
  55. }
  56. return {
  57. active: props.question.reply == index
  58. }
  59. }
  60. }
  61. function formatData(val) {
  62. if (val) {
  63. data.name = val.name;
  64. data.contents = val.content.map((item, index) => {
  65. return {
  66. label: item,
  67. number: getLetterByIndex(index)
  68. }
  69. })
  70. }
  71. }
  72. function onSelect(index) {
  73. if (props.showError) {
  74. return;
  75. }
  76. props.question.reply = index;
  77. emits('change')
  78. }
  79. </script>