Vue中如何使用Map鍵值對(duì)傳參詳析
Vue里使用Map鍵值對(duì)傳參
問題描述:在參數(shù)傳遞時(shí)時(shí)候Map鍵值對(duì)key:value的形式進(jìn)行傳參。
1、date數(shù)據(jù)區(qū)定義聲明map變量和中間數(shù)據(jù)變量temp:
data(){ return{ //其他代碼 map:'', temp:[] } },
2、methods方法區(qū)接口傳值:聲明map為Map變量,接收接口傳過來的key數(shù)據(jù)生成表單項(xiàng)數(shù)據(jù)項(xiàng):
this.map=new Map()
即:
//點(diǎn)擊待辦任務(wù),去處理待辦任務(wù) 填寫待辦表單 goDealTaskShow(index){ this.$fetchPost('你的接口',{taskID:this.taskForm.taskId}).then(res=>{ this.getTaskForm = res.data; this.map = new Map() for (var i=0;i<this.getTaskForm.length;i++){ this.map.set(this.getTaskForm[i].fieldName,''); } console.log(this.getTaskForm,'集合集合') console.log(this.map,'map集合') }) this.taskNameList.push(this.$refs.taskName[index].innerHTML) this.taskIdList.push(this.$refs.taskId[index].innerHTML) },
3、form表單數(shù)據(jù)綁定,且根據(jù)for循環(huán)index值用temp數(shù)據(jù)進(jìn)行數(shù)據(jù)綁定。
<el-form-item v-for="(item,index) in map" :key="item[0]" :label="item[0]"> <div class="dealTaskItem"> <el-input @input="updateForce($event)" v-model ="temp[index]" class="dateTaskCss" placeholder="請(qǐng)輸入" clearable @change="mapUpdate(item[0],index)"></el-input> </div> </el-form-item>
4、mapUpdate方法將temp數(shù)組接收的值對(duì)Map進(jìn)行key:value傳值:
mapUpdate(key,index){ this.map.set(key,this.temp[index]); console.log(key) console.log(this.map) }
5、mapToJson方法對(duì)map進(jìn)行json數(shù)據(jù)格式轉(zhuǎn)換:
mapToJson(map) { return JSON.stringify(this.strMapToObj(map)); },
6、map數(shù)據(jù)賦值給接口參數(shù),進(jìn)行傳參:
saveDealTaskForm(){ this.$forceUpdate(); this.saveTaskForm.result=this.mapToJson(this.map); console.log( this.mapToJson(this.map),' mapToJson(this.map)') this.$fetchPost('你的接口',this.saveTaskForm,'json').then(res=>{ if (res.code===0){ this.$message({ message:res.data, type:'success', }) this.initEvents();//數(shù)據(jù)刷新 }else{ this.$message.error("處理失?。?) } }) this.dealTaskVisible = false;//關(guān)閉表單彈窗 },
搞定,嘻嘻!
補(bǔ)充:vue遍歷Map,Map在vue中的使用方法
Map在vue中的使用方法:
html:遍歷的時(shí)候要遍歷兩遍
<template> <div class="course"> <div class=""> <div class="pt0 ctRow"> <ul class="ctNavbar"> <li class="ctA" v-for="(item,index) in courseTypeList" :key="item.id" @click="selectedCur(index)" :class="{ cur:cur == index }" :data-id="item.id" > {{item.courseTypeName}} </li> </ul> </div> <!--Map遍歷兩遍 --> <div class="ctRow date_navbar"> <a href="javascript:;" v-for="(item,index) in dateMap" :key="index" @click="dataCur(index)" :class="{ cur:active == index }" > <span v-for="data in item">{{data}}</span> </a> </div> </div> </div> </template>
js:
data(){ return{ cur: "0", active:"0", courseTypeList: [], dateMap:{}, } }, mounted(){ // 后端返回json var jsonStr ={ "code": 0, "msg": "success", "data": { "courseTypeList": [ { "id": "16", "courseTypeName": "小一班" }, { "id": "15", "courseTypeName": "中一班" }, { "id": "14", "courseTypeName": "大一班" } ], "dateKeys": [ "二·11.26", "三·11.27", "四·11.28", "五·11.29", "六·11.30", "日·12.01", "一·12.02" ] } } // 遍歷班級(jí)類型 for (var i = 0; i < jsonStr.data.courseTypeList.length; i++) { var courseTypeList = jsonStr.data.courseTypeList[i]; this.courseTypeList.push(courseTypeList) } // 遍歷日期 //初始化Map對(duì)象 var dateMap = new Map(); for (var i = 0; i < jsonStr.data.dateKeys.length; i++) { var data = jsonStr.data.dateKeys[i]; //用split連在一起的字符串切割 "三·11.27" //使用set添加鍵值示例:m.set('小紅', 30); dateMap.set(data.split("·")[0], data.split("·")[1]); } this.dateMap = dateMap; },
css:
.ctNavbar{ display: flex; align-items: center; justify-content: space-between; text-align: center; .ctA{ padding: 10px 0; color: #202020; font-size: 1.5rem !important; border-bottom: 2px solid transparent; width: 33%; &.cur{ color: #BA0932; border-bottom: 2px solid #BA0932; } } } .date_navbar { width: 100%; display: flex; justify-content: space-between; align-items: center; a { width: 32px; -webkit-box-flex: 1; display: flex; justify-content: space-between; align-items: center; flex-direction: column; font-size: 9px; color: #6C6C6C; padding: 2px 5px; flex: 1; span { display: block; -webkit-box-flex: 1; } &.cur { background: #BA0932; color: #fff; border-radius: 16px; } } }
總結(jié)
到此這篇關(guān)于Vue中如何使用Map鍵值對(duì)傳參的文章就介紹到這了,更多相關(guān)Vue用Map鍵值對(duì)傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue element-ui實(shí)現(xiàn)input輸入框金額數(shù)字添加千分位
這篇文章主要介紹了vue element-ui實(shí)現(xiàn)input輸入框金額數(shù)字添加千分位,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12vue中異步數(shù)據(jù)獲取方式(確保數(shù)據(jù)被獲取)
這篇文章主要介紹了vue中異步數(shù)據(jù)獲取方式(確保數(shù)據(jù)被獲取),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子
今天小編就為大家分享一篇vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue格式化數(shù)據(jù)后切換頁(yè)面出現(xiàn)NaN問題及解決
這篇文章主要介紹了Vue格式化數(shù)據(jù)后切換頁(yè)面出現(xiàn)NaN問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06解決vue頁(yè)面刷新vuex中state數(shù)據(jù)丟失的問題
這篇文章介紹了解決vue頁(yè)面刷新vuex中state數(shù)據(jù)丟失的問題,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Vue使用lodop實(shí)現(xiàn)打印小結(jié)
這篇文章主要介紹了Vue使用lodop打印小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07vue的簡(jiǎn)介及@vue/cli?腳手架的使用示例
vue 是一個(gè) 漸進(jìn)式的javascript框架,腳手架是一個(gè)通用概念,幫助搭建項(xiàng)目的工具,本文以vue2為例結(jié)合實(shí)例代碼給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2022-12-12