對VUE中的對象添加屬性
背景:在通過接口獲取數(shù)據(jù)集對象后,根據(jù)業(yè)務(wù)場景需要在數(shù)據(jù)集對象上增加額外的屬性
data中定義的數(shù)據(jù)集對象mindData格式示例如下
mindData: [
{label:'清醒',value:'清醒'}, {label:'朦朧',value:'朦朧'},
{label:'嗜睡',value:'嗜睡'}, {label:'昏睡',value:'昏睡'},
{label:'譫妄',value:'譫妄'}, {label:'模糊',value:'模糊'}]
1) 通過post調(diào)用接口獲取minData對象,遍歷添加屬性value和content(方便后續(xù)通過v-model設(shè)置綁定radio控件的選擇結(jié)果值value)
this.$http.post('XXXXXXXXXXXXXXXXXXXXXXXX', {
parms:'xxx'
}).then(res => {
letsel= this
sel.mindData= res.data
for(letitemofsel.mindData) {
item.value= ''
item.content=''
}
})
2) 這里我自定義了radio控件,部分代碼如下
<mt-cell:title="label"class="zm-radio mint-field">
<input:placeholder="placeholder"
type="text"
:readonly="!editable"
style="margin-right: 14px;"
v-model="currentContent"
@click="onHandleClick"
class="mint-field-core"/>
<spanclass="mintui mintui-back reset" @click="popupVisible=true"></span>
<mt-popupclass="zm-radio-popup"
position="bottom"
v-model="popupVisible"
popup-transition="popup-fade"
:style="{height:popupHeight}"
ref="pop">
<zm-container>
<zm-mainref="zmRadioMain">
<div@click="popupVisible=false">
<mt-radiostyle="width: 100%"
:title="label"
align="right"
v-model="currentValue"
:options="options">
</mt-radio>
</div>
</zm-main>
</zm-container>
</mt-popup>
</mt-cell>
export default{
watch: {
popupVisible() {
this.options= this.dictItems
this.currentValue= this.value
letheight= this.options.length * 48
letmaxHeight= window.innerHeight * 0.5
if(height> maxHeight) {
this.popupHeight= maxHeight+ 'px'
letscrollHeight= maxHeight* maxHeight/ height
this.$refs.zmRadioMain.setScroll(scrollHeight,window.innerWidth)
}
},
currentValue() {
console.log('radio_currentValue:'+ this.currentValue)
this.$emit('input',this.currentValue)
letcontent= this.content
letlabel= ''
for(letitemof this.options) {
if(_.isEqual(item.value,this.currentValue)) {
label= item.label
break
}
}
this.currentContent= content
}
3) 綁定到自定義的radio控件上
<zm-radiolabel="單選:" :editable="false" :dict-data="mindData" :content.sync="data.content" v-model="data.value"></zm-radio>
賦值的關(guān)鍵代碼如下
watch: {
popupVisible() {
this.options= this.dictItems
this.currentValue= this.value
彈出選項框列表的時候,會把當(dāng)前文本上的value值賦值給currentValue對象,這樣下拉框就會自動定位顯示原先的選項值,期望達(dá)到的效果如下

乍看之下,沒什么問題,運(yùn)行后發(fā)現(xiàn)

點擊下拉框,彈出選項列表,怎么數(shù)據(jù)沒有通過v-model綁定上去,并且radio的value和lable值一直是空

搗鼓了很久,測試發(fā)現(xiàn)通過定義mindRadio對象的方式綁定在zm-radio對象上,顯示效果是能獲得期望結(jié)果,那問題很明顯,對象屬性的創(chuàng)建有問題
<zm-radiolabel="單選:"
:editable="false"
:dict-data="mindData"
:content.sync="mindRadio.content"
v-model="mindRadio.value"></zm-radio>
data() {
return{
mindRadio: {
code:'',
value:''
}
}
經(jīng)過vue官方資料查詢,提供了vue.set方法,通過以下方法解決了設(shè)置對象屬性的問題

this.$http.post('XXXXXXXXXXXXXXXXXXXXXXXX', {
parms:'xxx'
}).then(res => {
letsel= this
sel.mindData= res.data
for(letitemofsel.mindData) {
sel.$set(item,'value','')
sel.$set(item,'content','')
}
})
總結(jié)原因:其實問題是vue實例對象不允許直接添加屬性或刪除屬性,需要通過set方式更新數(shù)據(jù)對象。
另一種實現(xiàn)方式,可以采用先給臨時對象tempData添加屬性,再賦值給mindData
以上這篇對VUE中的對象添加屬性就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+WebSocket頁面實時刷新長連接的實現(xiàn)
最近vue項目要做數(shù)據(jù)實時刷新,數(shù)據(jù)較大,會出現(xiàn)卡死情況,所以本文主要介紹了頁面實時刷新長連接,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
使用Electron打包vue文件變成exe應(yīng)用程序的全過程
這篇文章主要給大家介紹了使用Electron打包vue文件變成exe應(yīng)用程序的全過程,文中通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2024-01-01

