情況一,字段類型不匹配
考慮option的value值的字段類型是否和api返回的字段類型一致,如果一個為字符串一個為數字類型是無法匹配上的
<template>
<div><el-select v-model="value" size="large"style="width: 240px"><el-optionv-for="item in options":key="item.value":label="item.label":value="item.value"/></el-select >
</div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
const options = [{value: 'Option1',label: '1',},{value: 'Option2',label: '2',},
]
const value = ref("")
const apiFun = async()=>{//接口返回的data結構/*{selectValue:1}*/const data = await axios.get("****url****")//解決方法,轉成同樣類型value.value =data.selectValue.toString()
}
</script>
情況二,響應式失效
字段類型能對上,但是響應式失效了,這個時候要使用key強制刷新組件
<template>
<div><el-select v-model="value" size="large":key="index"style="width: 240px"><el-optionv-for="item in options":key="item.value":label="item.label":value="item.value"/></el-select >
</div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
const options = [{value: 'Option1',label: '1',},{value: 'Option2',label: '2',},
]
const value = ref("")
const index = ref(0)
const apiFun = async()=>{//接口返回的data結構/*{selectValue:"2"}*/const data = await axios.get("****url****")value.value =data.selectValue//解決方法,強制刷新組件index +=1
}
</script>