nextTick-function.uvue 2.4 KB
Newer Older
crlfe's avatar
crlfe 已提交
1 2
<template>
  <view class="page">
crlfe's avatar
crlfe 已提交
3 4
    <view class="rect" :style="{width: width, height: height}"></view>
    <view class="row"></view>
crlfe's avatar
crlfe 已提交
5
    <view class="row">
crlfe's avatar
crlfe 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19
      <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>
crlfe's avatar
crlfe 已提交
20
    </view>
crlfe's avatar
crlfe 已提交
21
    <next-tick-child class="next-tick-child"></next-tick-child>
crlfe's avatar
crlfe 已提交
22 23 24 25
  </view>
</template>

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

crlfe's avatar
crlfe 已提交
28
export default {
crlfe's avatar
crlfe 已提交
29 30 31
  components: {
    nextTickChild: child
  },
crlfe's avatar
crlfe 已提交
32 33
  data () {
    return {
crlfe's avatar
crlfe 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      value: 0,
      width: 100,
      height: 100,

      nextTickBeforeWidth: 0,
      nextTickBeforeHeight: 0,
      nextTickAfterWidth: 0,
      nextTickAfterHeight: 0,
    }
  },
  mounted () {
    this.nextTickCallback()
    this.nextTickPromise()
  },
  methods: {
    nextTickCallback (): void {
      this.width = 200

      uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
        const rect = ret[0] as NodeInfo

        this.$nextTick(() => {
          uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
            const rect = ret[0] as NodeInfo
            this.nextTickAfterWidth = rect!.width!
          })
        })

        this.nextTickBeforeWidth = rect!.width!
      });
    },
    nextTickPromise (): void {
      this.height = 200

      uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
        const rect = ret[0] as NodeInfo

        this.$nextTick().then(() => {
          uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
            const rect = ret[0] as NodeInfo
            this.nextTickAfterHeight = rect!.height!
          })
        })

        this.nextTickBeforeHeight = rect!.height!
      });
crlfe's avatar
crlfe 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }
  }
}
</script>

<style>
.page {
  font-size: 16px;
}

.row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-bottom: 10px;
}
crlfe's avatar
crlfe 已提交
96 97 98 99

.rect {
  background-color: red;
}
crlfe's avatar
crlfe 已提交
100
</style>