Vue2基础篇-31-nextTick

1. 简介

  1. 语法:this.$nextTick(回调函数)
  2. 作用:在下一次 DOM 更新结束后执行其指定的回调。
  3. 什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。

2. demo

<template>
  <li>
    <label>
      <input
        type="checkbox"
        :checked="sendTodoObj.done"
        @change="handleCheck(sendTodoObj.id)"
      />

      <!-- 控制编辑的时候是否显示文字还是输入框 @blur为离开焦点触发事件 -->
      <span v-show="!sendTodoObj.Edit">{{ sendTodoObj.title }}</span>
      <input
        type="text"
        v-show="!sendTodoObj.Edit"
        :value="sendTodoObj.title"
        @blur="handleBlur(sendTodoObj, e)"
      />
    </label>
    <button class="btn btn-danger" @click="todoDelete(sendTodoObj.id)">
      删除
    </button>
    <button class="btn btn-edit" @click="todoEdit(sendTodoObj)">更新</button>
  </li>
</template>

<script>
export default {
  name: "MyItem",
  props: ["sendTodoObj"],
  methods: {
    //勾选or取消勾选
    handleCheck(id) {
      this.$bus.$emit("checkTodo", id);
    },
    //删除
    todoDelete(id) {
      if (confirm("确定删除吗?")) {
        //通知App删除
        this.$bus.$emit("deleteTodoObj", id);
      }
    },
    //更新
    todoEdit(sendTodoObj) {
      // hasOwnProperty判断对象上是否包含此属性
      console.log("todoEdit", sendTodoObj);
      if (sendTodoObj.hasOwnProperty("isEdit")) {
        sendTodoObj.isEdit = true;
      } else {
        this.$set(sendTodoObj, "isEdit", true);
      }
    },
    //失去焦点时回调(真正执行修改逻辑)
    handleBlur(sendTodoObj, e) {
      console.log("handleBlur", sendTodoObj);
      sendTodoObj.isEdit = false;
      this.$bus.$emit("updateTodoObj", sendTodoObj.id, e.target.value);

      this.$nextTick(function () {
        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 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:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

li:hover {
  background-color: #ddd;
}

li:hover button {
  display: block;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan0517

感谢您的鼓励与支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值