1. 程式人生 > 實用技巧 >vue使用select間相互繫結

vue使用select間相互繫結

    

讓這兩個select相互繫結,讓roleOptions選取值後,worklist彈出得是roleOptions值

<el-select v-model="postForm.projectName" placeholder="請選擇" @change="getList(postForm)"> <el-option v-for="item in roleOptions" :key="item.key" :label="item.label" :value="item.key"> </el-option> <el-select v-model="postForm" placeholder="請選擇" value-key="id" @change="getList2(postForm)">
<el-option v-for="item in worklist" :label="item.productName" :value="item"> </el-option> 首先在created裡面獲取值,設res為null,傳入getlist中; async created() { this.lastWorklist = await api_price_list({},this.queryParam); let res = null; this.getList(res) }, 然後在methods中進行判斷 methods: { async getList(res) {
  this.listLoading = true   如果res為null獲取worklist   if(res != null){  this.worklist = [];    如果res裡面的獲取資料為s或y則繫結不同的值 if(res.projectName == "s"){ this.lastWorklist.data.list.forEach(item => { if(item.app == res.projectName){ this.worklist.push(item); } }); }  else if(res.projectName == "y"){
  this.lastWorklist.data.list.forEach(item => {   if(item.app == res.projectName){   this.worklist.push(item);   }   });   }  } this.listLoading = false }, 然後在getlist2裡面在第二個select元件進行傳值繫結 getList2(res){ if(res.app=="s"){ this.postForm.projectName = "抖音"; } else if(res.app == "y"){ this.postForm.projectName = "快手"; } },