1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <template>
- <view v-if="question">
- <!-- 标题区域 -->
- <view>单选题:</view>
- <!-- 题干区域 -->
- <view v-html="data.name"></view>
- <!-- 选项区域 -->
- <view>
- <view v-for="(item,index) in data.contents" :class="formatClass(index)" :key="index" @click="onSelect(index)">
- <text>{{item.number}}</text>
- <text>{{item.label}}</text>
- </view>
- </view>
- </view>
- </template>
- <script setup>
- import {
- ref,
- reactive,
- watch
- } from 'vue';
- import {
- useQuestionTools
- } from "./useQuestionTools"
- const {
- getLetterByIndex
- } = useQuestionTools();
- const props = defineProps({
- question: {
- type: Object,
- },
- showError: {
- type: Boolean,
- default: false
- }
- })
- const data = reactive({
- name: '', //题干数据
- contents: [], // 选项数据
- })
- watch(() => props.question, (val) => formatData(val), {
- immediate: true
- })
- function formatClass(index) {
- if (props.showError) {
- return {
- active_right: props.question.result == index,
- showError: props.question.reply == index && props.question.result != index
- }
- } else {
- return {
- active: props.question.reply == index
- }
- }
- }
- function formatData(val) {
- if (val) {
- data.name = val.name;
- data.contents = val.optList.map((item, index) => {
- return {
- label: item,
- number: getLetterByIndex(index)
- }
- })
- }
- }
- function onSelect(index) {
- if (props.showError) {
- return;
- }
- props.question.reply = index;
- }
- </script>
- <style lang="scss" scoped>
- .active {
- background-color: blue; // 单选题选中的颜色
- }
- .showError {
- background-color: red; // 答案解析:单选错误选中颜色
- }
- .active_right {
- background-color: green; // 答案解析:单选正确颜色
- }
- </style>
|