轉自:https://yuyuye958.github.io/2019/03/10/el-autocomplete/
問題
<el-autocomplete v-model="state" :fetch-suggestions="querySearchAsync" placeholder="請輸入內容" @select="handleSelect" ></el-autocomplete>
這是 Element UI 官方文檔中 el-autocomplete 的示例,而這里的 handleSelect 默認綁定的參數是你選中的那條數據。
但是如果你需求用 v-for 生成多個組件,要把 index 給傳進這個方法,你可能會這樣做:
@select="handleSelect(item, index)"
經試驗這是行不通的,那么該如何做呢?
方法
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearchAsync"
placeholder="請輸入內容"
@select="((item) => {handleSelect(item, index)})"
></el-autocomplete>
這樣你就能在 handleSelect 方法中拿到 index 參數了。
