==和===区别,注意false=='0'为true

2021-03-18  本文已影响0人  _信仰zmh
false=='0' //true

前台展示勾选框的时候,用到了checkbox,true为选中状态,false为取消状态。

<a-row class='label-row'>
            <a-col :span="6" class='label-text-left'>
              词根词性:
            </a-col>
            <a-col :span="18" style="padding-top:3px;">
              <a-checkbox :disabled='item.noPass'  style="margin-left:5px;" v-model="item.nounIdentification" @click="changeCheckBox(item,'nounIdentification')">名词</a-checkbox>
              <a-checkbox :disabled='item.noPass'  v-model="item.actionIdentification"  @click="changeCheckBox(item,'actionIdentification')">动词</a-checkbox>
              <a-checkbox :disabled='item.noPass'  v-model="item.adjectiveIdentification"  @click="changeCheckBox(item,'adjectiveIdentification')">形容词</a-checkbox>
              <a-checkbox :disabled='item.noPass'  v-model="item.otherIdentification"  @click="changeCheckBox(item,'otherIdentification')">其他</a-checkbox>
            </a-col> 
</a-row>

后台保存的时候,要转数据格式,'0'代表选中,'1'代表未选中,详情接口返回的数据也是'0'或'1'。

那么前台就需要转成相应的数据。

  watch: {
    rootList:{
      handler(val){
        this.drawRootList = val;
        for(let item of this.drawRootList){
          item.cname = item.cname?item.cname:'';
          item.nounIdentification = item.nounIdentification===true||item.nounIdentification==='0'?true:false;
          item.actionIdentification = item.actionIdentification===true||item.actionIdentification==='0'?true:false;
          item.adjectiveIdentification = item.adjectiveIdentification===true||item.adjectiveIdentification==='0'?true:false;
          item.otherIdentification = item.otherIdentification===true||item.otherIdentification==='0'?true:false;
        }
      },
      deep:true
    }
  },
  if(this.rootList&&this.rootList.length>0){
        let arr = [];
        for(let item of this.rootList){
          let obj = {};
          obj.ename = item.ename;
          obj.cname = item.cname;
          obj.nounIdentification = item.nounIdentification===true||item.nounIdentification==='0'?'0':'1';
          obj.actionIdentification = item.actionIdentification===true||item.actionIdentification==='0'?'0':'1';
          obj.adjectiveIdentification = item.adjectiveIdentification===true||item.adjectiveIdentification==='0'?'0':'1';
          obj.otherIdentification = item.otherIdentification===true||item.otherIdentification==='0'?'0':'1';
          obj.insideRootType = '1';
          obj.rootType = '2';
          obj.dataFieldId = this.tableInfo.dataFieldId;
          obj.creator = _user.cname + "(" + _user.name + ")";
          arr.push(obj);
        }
        eiInfo.set("rootList", arr);
      }

如果用=='0',不用==='0',因为false=='0'也为true,那么保存的时候无论你是true还是false都会保存成'0'

          obj.nounIdentification = item.nounIdentification===true||item.nounIdentification=='0'?'0':'1';
          obj.actionIdentification = item.actionIdentification===true||item.actionIdentification=='0'?'0':'1';
          obj.adjectiveIdentification = item.adjectiveIdentification===true||item.adjectiveIdentification=='0'?'0':'1';
          obj.otherIdentification = item.otherIdentification===true||item.otherIdentification=='0'?'0':'1';

这也是稍不注意就会出现问题的一个小bug。

关联拓展:js基本数据在操作的时候会发生显式/隐式数据类型转换,这个博主写的有兴趣了解的看看

上一篇 下一篇

猜你喜欢

热点阅读