写一个el-select对后端传过来的数组进行下拉展示的代码
时间: 2024-03-02 12:52:55 浏览: 27
好的,以下是一个简单的示例代码:
```html
<template>
<el-select v-model="selectedOption" placeholder="请选择">
<el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"></el-option>
</el-select>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
options: [],
selectedOption: ''
}
},
mounted() {
axios.get('/api/options')
.then(response => {
this.options = response.data
})
.catch(error => {
console.log(error)
})
}
}
</script>
```
这段代码中,我们使用 `axios` 从后端获取选项数据,并将其存储在 `options` 数组中。然后,我们在模板中使用 `el-select` 标签来展示选项,并将 `options` 绑定到 `el-option` 标签上。用户选择一个选项后,其值将存储在 `selectedOption` 变量中。