当前位置:首页 > VUE

vue实现多填空

2026-01-16 00:07:09VUE

实现多填空功能

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

动态表单绑定

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

vue实现多填空

<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>

填空题与文本混合

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

vue实现多填空

<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组件的实现

Vue 组件的实现方式 Vue 组件可以通过多种方式实现,以下是常见的几种方法: 单文件组件 (SFC) 使用 .vue 文件,包含模板、脚本和样式三部分: <template>…

vue实现单页面

vue实现单页面

Vue 实现单页面应用(SPA) Vue.js 是一个渐进式框架,非常适合构建单页面应用(SPA)。SPA 的核心特点是整个应用只有一个 HTML 页面,通过动态加载内容实现页面切换,无需重新加载整个…

vue实现按卡片轮播

vue实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-fo…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <templ…

vue实现继承语法糖

vue实现继承语法糖

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