vxe-radio-group 接收后端string类型数据 不立即回显
vxe-radio-group 组件可以接收后端返回的 string 类型数据,并且不会立即回显。需要将后端返回的数据通过 v-model 绑定到组件上,然后在组件的 mounted 生命周期钩子中手动设置选中的值,以实现数据的回显。具体实现步骤如下:
- 在 Vue 组件中引入 vxe-radio-group 组件,并在 template 中使用:
<template>
<vxe-radio-group v-model="selected" :options="options"></vxe-radio-group>
</template>
<script>
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
import { RadioGroup } from 'vxe-table/lib/components'
export default {
components: {
'vxe-radio-group': RadioGroup
},
data() {
return {
selected: '',
options: [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
]
}
},
mounted() {
// 在 mounted 生命周期钩子中手动设置选中的值
this.selected = this.backendData
}
}
</script>
- 将后端返回的数据通过 v-model 绑定到组件上:
<template>
<vxe-radio-group v-model="selected" :options="options"></vxe-radio-group>
</template>
<script>
export default {
data() {
return {
selected: '', // 通过 v-model 绑定选中的值
options: [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
]
}
}
}
</script>
- 在组件的 mounted 生命周期钩子中手动设置选中的值,以实现数据的回显:
<template>
<vxe-radio-group v-model="selected" :options="options"></vxe-radio-group>
</template>
<script>
export default {
data() {
return {
selected: '',
options: [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
]
}
},
mounted() {
// 在 mounted 生命周期钩子中手动设置选中的值
this.selected = this.backendData
}
}
</script>
``
原文地址: http://www.cveoy.top/t/topic/d5rP 著作权归作者所有。请勿转载和采集!