当前位置:首页 > VUE

vue实现多填空

2026-01-16 00:07:09VUE

实现多填空功能

在Vue中实现多填空功能可以通过动态绑定和循环渲染来实现。以下是几种常见的实现方式:

动态表单绑定

使用v-for指令动态生成填空输入框,并通过v-model绑定到数组或对象上:

<template>
  <div>
    <div v-for="(blank, index) in blanks" :key="index">
      <input v-model="blank.value" :placeholder="`填空 ${index + 1}`" />
    </div>
    <button @click="addBlank">添加填空</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      blanks: [{ value: '' }, { value: '' }]
    }
  },
  methods: {
    addBlank() {
      this.blanks.push({ value: '' })
    }
  }
}
</script>

填空题与文本混合

如果需要在一段文本中嵌入多个填空,可以使用模板字符串和动态组件:

<template>
  <div>
    <p v-html="processedText"></p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '{{1}}是中国的首都,{{2}}是最大的城市',
      answers: ['北京', '上海']
    }
  },
  computed: {
    processedText() {
      return this.text.replace(/\{\{(\d+)\}\}/g, (match, p1) => {
        const index = parseInt(p1) - 1
        return `<input v-model="answers[${index}]" />`
      })
    }
  }
}
</script>

使用组件封装

可以创建一个可复用的填空组件:

<template>
  <div class="fill-blank">
    <span v-for="(part, index) in parts" :key="index">
      <span v-if="part.type === 'text'">{{ part.content }}</span>
      <input v-else v-model="answers[part.index]" />
    </span>
  </div>
</template>

<script>
export default {
  props: {
    template: String
  },
  data() {
    return {
      answers: []
    }
  },
  computed: {
    parts() {
      const regex = /\{\{(\d+)\}\}/g
      let parts = []
      let lastIndex = 0
      let match

      while ((match = regex.exec(this.template)) !== null) {
        if (match.index > lastIndex) {
          parts.push({
            type: 'text',
            content: this.template.slice(lastIndex, match.index)
          })
        }

        parts.push({
          type: 'blank',
          index: parseInt(match[1]) - 1
        })

        lastIndex = match.index + match[0].length
      }

      if (lastIndex < this.template.length) {
        parts.push({
          type: 'text',
          content: this.template.slice(lastIndex)
        })
      }

      return parts
    }
  }
}
</script>

使用第三方库

对于更复杂的需求,可以考虑使用专门的表单库如VeeValidate或Vuelidate来增强填空功能:

<template>
  <div>
    <ValidationObserver>
      <div v-for="(blank, index) in blanks" :key="index">
        <ValidationProvider rules="required" v-slot="{ errors }">
          <input v-model="blank.value" />
          <span>{{ errors[0] }}</span>
        </ValidationProvider>
      </div>
    </ValidationObserver>
  </div>
</template>

以上方法可以根据具体需求选择或组合使用,实现灵活的多填空功能。

vue实现多填空

标签: vue
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vu…

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…