Vue.$set 失效的坑 問題發(fā)現(xiàn)及解決方案
偶然在項目中發(fā)現(xiàn)Vue.$set失效

有這樣一個需求 添加數(shù)據(jù)過濾用 左邊是控件選擇 中間是條件 右邊是值
因為會根據(jù)控件不同渲染不同的值選項控件
<el-form inline >
<el-form-item style="margin-bottom: 20px;">
<el-select v-model="data[props.prop]" @change="data[props.value] = ''">
<el-option
v-for="item in controls"
:key="item.id"
:value="item.id"
:label="item.label">
</el-option>
</el-select>
</el-form-item>
<el-form-item style="margin-bottom: 20px;">
<el-select v-model="data[props.type]">
<el-option
v-for="item in condition"
:key="item.code"
:value="item.code"
:label="item.name"
></el-option>
</el-select>
</el-form-item>
<el-form-item style="margin-bottom: 20px;">
<FormControl v-if="control" :control="control" :value="data[props.value]" @input="onValueChange" ></FormControl>
<el-input v-else :value="data[props.value]" @input="onValueChange"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
</el-form-item>
</el-form>
{
props:{
props: {
type: Object,
default: () => ({
prop: 'prop',
value: 'value',
type: 'type'
})
}
},
data(){
return {
data:{
}
}
},
methods:{
onValueChange(val){
this.$set(this.data, this.props.value, val)
}
}
//...
}
代碼片段
由于控件ID的不確定性 所有 data并不能提前預(yù)設(shè)好key 自然無法響應(yīng) 所以在onValueChange 使用了this.$set動態(tài)添加數(shù)據(jù)實現(xiàn)響應(yīng)

復(fù)現(xiàn)可以發(fā)現(xiàn) 值輸入框內(nèi)的數(shù)據(jù)并不能實時響應(yīng)
明明用了$set卻不能響應(yīng) 一番排查后發(fā)現(xiàn)只要切換控件后 value值就不能響應(yīng) 但是只要在切換前隨便輸入點啥 再切換就沒問題
又是一番排查后發(fā)現(xiàn)
<el-select v-model="data[props.prop]" @change="data[props.value] = ''">
刪除@change事件后故障解決
問題出現(xiàn)在 data[props.value] = ''
遂查看Vue源碼
//vue/src/core/observer/index.js 源碼片段
/**
* Set a property on an object. Adds the new property and
* triggers change notification if the property doesn't
* already exist.
*/
export function set (target: Array<any> | Object, key: any, val: any): any {
if (process.env.NODE_ENV !== 'production' &&
(isUndef(target) || isPrimitive(target))
) {
warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`)
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.length = Math.max(target.length, key)
target.splice(key, 1, val)
return val
}
if (key in target && !(key in Object.prototype)) {
target[key] = val
return val
}
const ob = (target: any).__ob__
if (target._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
'Avoid adding reactive properties to a Vue instance or its root $data ' +
'at runtime - declare it upfront in the data option.'
)
return val
}
if (!ob) {
target[key] = val
return val
}
defineReactive(ob.value, key, val)
ob.dep.notify()
return val
}
可以發(fā)現(xiàn) 在defineReactive之前 判斷了key是否存在于對象之內(nèi) 若存在就跳過了
坑就在這 多次翻閱Vue.$set文檔并未發(fā)現(xiàn)$set不能為已存在的key添加監(jiān)測對象
刪除 data[props.value] = '' 改為 onValueChange('') 完美解決問題

總結(jié)
Vue.$set之前一定要對象內(nèi)key不存在 不然只會更新值 并不會為該Key添加響應(yīng)監(jiān)測
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中watch監(jiān)聽屬性新舊值相同的問題解決方案
這篇文章主要給大家分享了Vue中watch監(jiān)聽屬性新舊值相同問題解決方案,如果有遇到相同問題的朋友,可以參考閱讀本文2023-08-08
Vue中slot-scope的深入理解(適合初學(xué)者)
這篇文章主要給大家介紹了關(guān)于Vue中slot-scope的深入理解,這個教程非常適合初學(xué)者,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
vue實現(xiàn)同一個頁面可以有多個router-view的方法
今天小編就為大家分享一篇vue實現(xiàn)同一個頁面可以有多個router-view的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue-cli中設(shè)置publicPath的幾種方式對比
這篇文章主要介紹了vue-cli中設(shè)置publicPath的幾種方式對比,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

