当前位置:首页 > VUE

vue实现单选弹窗

2026-01-16 01:55:22VUE

Vue 实现单选弹窗的方法

使用 Element UI 的 Dialog 和 Radio 组件

安装 Element UI 后,可以通过 Dialog 和 Radio 组件快速实现单选弹窗功能。

vue实现单选弹窗

<template>
  <div>
    <el-button @click="dialogVisible = true">打开单选弹窗</el-button>

    <el-dialog title="请选择" :visible.sync="dialogVisible">
      <el-radio-group v-model="selectedOption">
        <el-radio v-for="option in options" :key="option.value" :label="option.value">
          {{ option.label }}
        </el-radio>
      </el-radio-group>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      selectedOption: '',
      options: [
        { value: 'option1', label: '选项1' },
        { value: 'option2', label: '选项2' },
        { value: 'option3', label: '选项3' }
      ]
    }
  },
  methods: {
    handleConfirm() {
      console.log('选中的选项:', this.selectedOption)
      this.dialogVisible = false
    }
  }
}
</script>

自定义单选弹窗组件

如果需要更灵活的控制,可以创建自定义单选弹窗组件。

vue实现单选弹窗

<template>
  <div class="custom-dialog" v-if="visible">
    <div class="dialog-content">
      <h3>{{ title }}</h3>
      <div class="radio-group">
        <div 
          v-for="option in options" 
          :key="option.value" 
          class="radio-item"
          @click="selectOption(option.value)"
        >
          <span :class="{ selected: selectedOption === option.value }"></span>
          {{ option.label }}
        </div>
      </div>
      <div class="dialog-footer">
        <button @click="cancel">取消</button>
        <button @click="confirm">确定</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    visible: Boolean,
    title: String,
    options: Array,
    value: String
  },
  data() {
    return {
      selectedOption: this.value
    }
  },
  methods: {
    selectOption(value) {
      this.selectedOption = value
    },
    cancel() {
      this.$emit('cancel')
    },
    confirm() {
      this.$emit('confirm', this.selectedOption)
    }
  }
}
</script>

<style scoped>
.custom-dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.dialog-content {
  background: white;
  padding: 20px;
  border-radius: 5px;
  width: 300px;
}

.radio-item {
  padding: 10px;
  cursor: pointer;
}

.radio-item span {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 1px solid #999;
  border-radius: 50%;
  margin-right: 10px;
}

.radio-item span.selected {
  background: #409EFF;
  border-color: #409EFF;
}

.dialog-footer {
  margin-top: 20px;
  text-align: right;
}
</style>

使用 Vuex 管理弹窗状态

对于复杂应用,可以使用 Vuex 来管理弹窗状态和选择结果。

// store.js
export default new Vuex.Store({
  state: {
    radioDialog: {
      visible: false,
      options: [],
      selected: null
    }
  },
  mutations: {
    showRadioDialog(state, options) {
      state.radioDialog.visible = true
      state.radioDialog.options = options
    },
    hideRadioDialog(state) {
      state.radioDialog.visible = false
    },
    selectOption(state, value) {
      state.radioDialog.selected = value
    }
  }
})

// 组件中使用
<template>
  <div>
    <button @click="showDialog">打开弹窗</button>
    <radio-dialog 
      v-if="$store.state.radioDialog.visible"
      :options="$store.state.radioDialog.options"
      :selected="$store.state.radioDialog.selected"
      @select="selectOption"
      @confirm="confirmSelection"
      @cancel="hideDialog"
    />
  </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  methods: {
    ...mapMutations(['showRadioDialog', 'hideRadioDialog', 'selectOption']),
    showDialog() {
      const options = [
        { value: '1', label: '选项1' },
        { value: '2', label: '选项2' }
      ]
      this.showRadioDialog(options)
    },
    confirmSelection() {
      console.log('选中:', this.$store.state.radioDialog.selected)
      this.hideDialog()
    }
  }
}
</script>

使用第三方插件 vue-js-modal

vue-js-modal 是一个流行的 Vue 模态框插件,可以方便地实现单选弹窗。

// 安装后
import VModal from 'vue-js-modal'
Vue.use(VModal)

// 组件中使用
<template>
  <div>
    <button @click="showModal">打开弹窗</button>

    <modal name="radio-modal">
      <h3>请选择</h3>
      <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>
      <button @click="hideModal">取消</button>
      <button @click="submit">确定</button>
    </modal>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { value: '1', label: '选项1' },
        { value: '2', label: '选项2' }
      ]
    }
  },
  methods: {
    showModal() {
      this.$modal.show('radio-modal')
    },
    hideModal() {
      this.$modal.hide('radio-modal')
    },
    submit() {
      console.log('选中:', this.selectedOption)
      this.hideModal()
    }
  }
}
</script>

以上方法提供了不同复杂度的实现方案,可以根据项目需求选择合适的方式来实现 Vue 单选弹窗功能。

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

相关文章

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提…

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-s…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 wi…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…