不积跬步无以至千里

102 阅读1分钟

判断数据是否为空

// var a = {};
// var a = NaN
if(a === undefined){ // 只能用=== 运算来测试某个值是否是未定义的
  console.log('为undefined')
}
if(a == null){ // 等同于a === undefined ||  a === null
  console.log('为null')
}
// string
if(a == '' || a ==null || a == undefined){ // '',null,undefined
  console.log('为空')
}
if(!a){ // '',null,undefined,NaN
  console.log('为空')
}

vue 中的watch的写法

watch:{
  name:{
     handler(newVal,oldVal){ 
     // do something 
     ...
     }
     immediate:true,
     deep:true
  }
}

vue中父组件传给子组件数据不更新(一种方法,解决办法就是用watch)

props:{
data:{
   type:string
}
},
created(){
  this.$set(this.data)
}

对数组中的数据循环,点击触发

<div v-for="(item,index) in list" :key="index" @click="clickItem(index)" :class="{red:i === index}">
  {{item}}
</div>
data(){
  return{
     i:''
  }
}
clickItem(index){
    this.i = index
}

提交参数时,有时可能不需要某一个参数删除操作

 var list = [{ attachmentId: '', imageLink: '', link: '', enabled: false, client: 2 }]
add(){
  list.forEach((item,index)=>{
    delete item.link
  })
  addData(list).then((res)=>{})
 
}