nextTick-function.uvue 2.7 KB
Newer Older
crlfe's avatar
crlfe 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
4
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5
    <view class="page">
6
      <view class="rect" :style="{ width: width + 'px', height: height + 'px' }"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
      <view class="row"></view>
      <view class="row">
        <text ref="text">$nextTick Before Width:</text>
        <text>{{ nextTickBeforeWidth }}</text>
      </view>
      <view class="row">
        <text ref="text">$nextTick Before Height:</text>
        <text>{{ nextTickBeforeHeight }}</text>
      </view>
      <view class="row">
        <text ref="text">$nextTick After Width:</text>
        <text>{{ nextTickAfterWidth }}</text>
      </view>
      <view class="row">
        <text ref="text">$nextTick After Height:</text>
        <text>{{ nextTickAfterHeight }}</text>
      </view>
      <next-tick-child class="next-tick-child"></next-tick-child>
crlfe's avatar
crlfe 已提交
25
    </view>
26
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28
  </scroll-view>
  <!-- #endif -->
crlfe's avatar
crlfe 已提交
29 30
</template>

crlfe's avatar
crlfe 已提交
31
<script lang="ts">
32
  import child from './child.uvue'
crlfe's avatar
crlfe 已提交
33

34 35 36
  export default {
    components: {
      nextTickChild: child
crlfe's avatar
crlfe 已提交
37
    },
38 39 40 41 42
    data() {
      return {
        value: 0,
        width: 100,
        height: 100,
crlfe's avatar
crlfe 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        nextTickBeforeWidth: 0,
        nextTickBeforeHeight: 0,
        nextTickAfterWidth: 0,
        nextTickAfterHeight: 0,
      }
    },
    mounted() {
      this.nextTickCallback()
      this.nextTickPromise()
    },
    methods: {
      nextTickCallback() : void {
        this.width = 100
        uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
          const rect = ret[0] as NodeInfo
          this.width = 200
          this.$nextTick(() => {
            uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
              const newRect = ret[0] as NodeInfo
              this.nextTickAfterWidth = newRect.width!
            })
crlfe's avatar
crlfe 已提交
65
          })
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
          this.nextTickBeforeWidth = rect.width!
        });
      },
      nextTickPromise() : void {
        this.height = 100
        uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
          const rect = ret[0] as NodeInfo
          this.height = 200
          this.$nextTick().then(() => {
            uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
              const newRect = ret[0] as NodeInfo
              this.nextTickAfterHeight = newRect.height!
            })
          })
          this.nextTickBeforeHeight = rect.height!
        });
      }
crlfe's avatar
crlfe 已提交
83 84 85 86 87
    }
  }
</script>

<style>
88 89 90 91 92 93
  .row {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-bottom: 10px;
  }
crlfe's avatar
crlfe 已提交
94

95 96 97 98
  .rect {
    background-color: red;
  }
</style>