Vue常用之DOM元素未更新时的回调“nextTick”

<template>
  <li>
    <label>
      <!-- 如下代码也能实现功能,但是不太推荐,因为有点违反原则,因为修改了props -->
      <!-- <input type="checkbox" v-model="todo.done"/> -->
    
      <input type="text" ref="inputTitle" v-show="todo.edit" :value="todo.title"  @blur="blurPrice(todo,$event)" @keyup.enter="keyupEnter(todo,$event)">
     
    </label>

    <button  class="btn btn-edit" @click="handleEdit(todo)">编辑</button>
  </li>
</template>

<script>


export default {
  name: "MyItem",
  // props:['todo','checkTodo'],
  props: {
    todo: {
      type: Object,
      required: true, //必要性
    },
    // checkTodo: {},
    // delTodo: {},
  },
  data() {
    return {
      todoItem: this.todo,
      // showHide:false,
    };
  },
  mounted() {
    // console.log(this.valueAll,'ss')
  },
  computed:{
    
  },
  
  methods: {

    handleEdit(todo){//编辑
      // this.todo.edit = true 不能这样添加要

      // if(todo.edit){//这个判断存在-重复调用问题
      if(todo.hasOwnProperty('edit')){//完美解决-重复调用问题
         todo.edit = true
      }else{
        console.log('身上没用edit')
         this.$set(todo,'edit',true)//set添加
        
      }
      // setTimeout(()=>{//因为inputDOM标签在执行这个代码的时候,还没有显示出来,所以加定时器
      // this.$refs.inputTitle.focus()//获取焦点
      // },100)

      this.$nextTick(()=>{//用的特别多,在下一次DOM更新结束后执行指定的回调
        this.$refs.inputTitle.focus()//获取焦点
      })
    },
   
    
    
  },
};
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li:hover {
  background: rgb(219, 216, 216);
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:hover button {
  display: block;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值