nextTick不等待渲染

时间:2018-11-06 11:13:04

标签: vue.js

为什么console.log下面的代码显示不同的值?

nextTick内的console.log显示1344

getWidth中的console.log显示728

如何解决?

<template>
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-sm-8">
        <div class="card">
          <div class="card-header bg-primary text-white">Teste</div>
          <div ref="card" class="card-body">
            Test get width of card-body
          </div>
        </div>
        <button @click="getWidth" class="btn btn-primary">OK</button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    mounted() {
      this.$nextTick(() => {
        console.log('Inside nextTick ' + this.$refs.card.clientWidth);
      });
    },
    methods: {
      getWidth() {
        console.log('getWidth ' + this.$refs.card.clientWidth)
      }
    }
  }
</script>

<style scoped lang="scss">

</style>

0 个答案:

没有答案