vue中判斷伺服器傳遞的資料是否存在 即 vue監聽一個不存在的變數
阿新 • • 發佈:2019-01-01
如果在專案中的某個方法中判斷伺服器端傳來的資料是否存在可以 採用 typeof 屬性 == "undefined" 判斷
例如 :
<div v-for="(item,index) in testData" :key="index">
<div :class="{"check": item.checked}"@click= "text(item)"></div>
</div>
data(){
return {
testData:[
{
name:
'a',
sex:'0'
}
]
}
},
methods:{
test(item){
if(typeof item.checked == "undefined"){
//alert('測試') 當不存在是 我們一定要使用這個屬性時
//方法一如下:在vue 全域性設定checked 屬性
Vue.set(item,"checked",true);
//方法二 : 在vue區域性設定checked 屬性
this.$set(item,"checked",true);
}else{
item.checked = !item.checked
}
}
}