当前位置:首页 > VUE

vue中单选怎样实现

2026-02-21 18:41:18VUE

Vue 中实现单选的方法

使用 v-model 绑定单选按钮

通过 v-model 绑定数据,可以轻松实现单选功能。单选按钮的 value 属性决定了选中时的值。

vue中单选怎样实现

<template>
  <div>
    <input type="radio" id="option1" value="option1" v-model="selectedOption" />
    <label for="option1">选项1</label>
    <input type="radio" id="option2" value="option2" v-model="selectedOption" />
    <label for="option2">选项2</label>
    <p>选中的选项: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: ''
    }
  }
}
</script>

使用 v-for 动态生成单选按钮

如果需要动态生成单选按钮,可以通过 v-for 遍历选项数组。

vue中单选怎样实现

<template>
  <div>
    <div v-for="option in options" :key="option.value">
      <input
        type="radio"
        :id="option.value"
        :value="option.value"
        v-model="selectedOption"
      />
      <label :for="option.value">{{ option.label }}</label>
    </div>
    <p>选中的选项: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { value: 'option1', label: '选项1' },
        { value: 'option2', label: '选项2' }
      ]
    }
  }
}
</script>

使用组件封装单选逻辑

如果需要复用单选逻辑,可以封装一个自定义单选组件。

<!-- RadioGroup.vue -->
<template>
  <div>
    <div v-for="option in options" :key="option.value">
      <input
        type="radio"
        :id="option.value"
        :value="option.value"
        v-model="internalValue"
      />
      <label :for="option.value">{{ option.label }}</label>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: String,
      default: ''
    },
    options: {
      type: Array,
      required: true
    }
  },
  computed: {
    internalValue: {
      get() {
        return this.value
      },
      set(newValue) {
        this.$emit('input', newValue)
      }
    }
  }
}
</script>

使用第三方库

如果需要更丰富的功能(如样式、分组等),可以使用第三方库如 element-uivant

<template>
  <div>
    <el-radio v-model="selectedOption" label="option1">选项1</el-radio>
    <el-radio v-model="selectedOption" label="option2">选项2</el-radio>
    <p>选中的选项: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: ''
    }
  }
}
</script>

标签: 单选vue
分享给朋友:

相关文章

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue sku 实现

vue sku 实现

Vue SKU 实现方案 在电商系统中,SKU(Stock Keeping Unit)是商品的最小库存单位,通常由多个属性组合而成(如颜色、尺寸等)。以下是基于 Vue 的实现方案。 数据结构设计…

vue实现菜单定位

vue实现菜单定位

实现菜单定位的方法 在Vue中实现菜单定位功能,可以通过监听滚动事件或使用Intersection Observer API来判断当前显示的菜单项,并高亮对应的导航链接。以下是几种常见的实现方式:…