Select 選擇器進行搜索
<template>
<div>
<a-form-item label="分類:">
<a-select
placeholder="請選擇"
style="width: 320px"
v-model:value="formState.sortValue"
:showSearch="true"
>
<a-select-option v-for="(item, index) in listArr" :key="index">
{{ item.name }}
</a-select-option>
</a-select>
</a-form-item>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive } from 'vue'
export default defineComponent({
setup() {
let formState = reactive({
sortValue: '',
})
let listArr = [
{ name: '華為', value: '001' },
{ name: '小米', value: '002' },
{ name: 'oppo', value: '003' },
]
return {
listArr,
formState,
}
},
})
</script>

發現搜索失敗的解決辦法
在 <a-select>上添加
optionFilterProp="label"
他表示搜索時過濾對應的 option 屬性,不支持 children
:label="item.name"
最終代碼為
<a-form-item label="分類:">
<a-select
placeholder="請選擇"
style="width: 320px"
v-model:value="formState.sortValue"
:showSearch="true"
optionFilterProp="label"
>
<a-select-option
:label="item.name"
v-for="(item, index) in listArr"
:key="index"
>
{{ item.name }}
</a-select-option>
</a-select>
</a-form-item>

處理Select滾動時不跟隨與select框分離
使用getPopupContainer函數
菜單渲染父節點。
默認渲染到 body 上,
如果你遇到菜單滾動定位問題,試試修改為滾動的區域,
並相對其定位。

解決辦法
<a-select
placeholder="請選擇"
style="width: 320px"
v-model:value="formState.sortValue"
:getPopupContainer="
triggerNode => {
return triggerNode.parentNode || document.body
}
"
>
<a-select-option
v-for="(item, index) in listArr"
:key="index"
>
{{ item.name }}
</a-select-option>
</a-select>

值類型錯誤回填失敗
需要的是字符串類型,
但是返回來的是一個數字類型導致回填失敗
描述:華為的value='10'字符串10
但是返回來的是一個數字類型的10
這樣回填會出現數字10,而不是回填華為
將數字類型更改為字符串類型就可以解決
類型錯誤的小例子
<template>
<div>
<a-form-item label="分類:">
<a-select
placeholder="請選擇"
style="width: 320px"
v-model:value="formState.sortValue"
>
<a-select-option
:label="item.name"
v-for="(item, index) in listArr"
:key="index"
>
{{ item.name }}
</a-select-option>
</a-select>
</a-form-item>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive } from 'vue'
export default defineComponent({
setup() {
let formState: any = reactive({
sortValue: 10,
})
let listArr = [
{ name: '華為', value: '10' },
{ name: '小米', value: '12' },
{ name: 'oppo', value: '13' },
]
return {
listArr,
formState,
}
},
})
</script>

數據不存在出現的問題
有些時候會出現這樣的情況,
返回來的數據值在下拉框中匹配不到,
此時就會回填返回來的值,但是我們並不需要出現這樣的情況
我們期望匹配不到回填空
解決辦法:將返回來的值與下拉框中的值進行匹配。
如果查找不到,直接回填空
這種方式需要在每一個使用了下拉框中的頁面寫方法
很不友好,最好的是從底層處理。給源碼一個配置項