nextTick

    在挂载阶段之前,我们是获取不到DOM节点及其节点上的数据的,如果我们想要在挂载阶段之前的函数中获取挂载后的数据,就需要使用$nextTick

    $nextTick有两种基本使用方法:

    1<template>
    2  <div>
    3    <p ref="op">这是p标签</p>
    4  </div>
    5</template>
    6
    7<script>
    8export default {
    9  data() {
    10    return {};
    11  },
    12  created() {
    13    // 这里并不会立即执行,等到mounted渲染完毕之后再调用
    14    // 写法一
    15    this.$nextTick(() => {
    16      console.log(this.$refs.op);
    17    });
    18    // 写法二
    19    this.$nextTick().then(() => {
    20      console.log(this.$refs.op);
    21    });
    22  },
    23};
    24</script>