nextTick-function.uvue 2.6 KB
Newer Older
crlfe's avatar
crlfe 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <view class="page">
      <view class="rect" :style="{ width: width, height: height }"></view>
      <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>
DCloud-WZF's avatar
DCloud-WZF 已提交
26 27 28
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
crlfe's avatar
crlfe 已提交
29 30
</template>

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

crlfe's avatar
crlfe 已提交
34
export default {
crlfe's avatar
crlfe 已提交
35 36 37
  components: {
    nextTickChild: child
  },
crlfe's avatar
crlfe 已提交
38 39
  data () {
    return {
crlfe's avatar
crlfe 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      value: 0,
      width: 100,
      height: 100,

      nextTickBeforeWidth: 0,
      nextTickBeforeHeight: 0,
      nextTickAfterWidth: 0,
      nextTickAfterHeight: 0,
    }
  },
  mounted () {
    this.nextTickCallback()
    this.nextTickPromise()
  },
  methods: {
    nextTickCallback (): void {
crlfe's avatar
crlfe 已提交
56
      this.width = 100
crlfe's avatar
crlfe 已提交
57 58 59

      uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
        const rect = ret[0] as NodeInfo
crlfe's avatar
crlfe 已提交
60 61 62
		
		this.width = 200
		
crlfe's avatar
crlfe 已提交
63 64
        this.$nextTick(() => {
          uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
crlfe's avatar
crlfe 已提交
65 66
            const newRect = ret[0] as NodeInfo
            this.nextTickAfterWidth = newRect.width!
crlfe's avatar
crlfe 已提交
67 68 69
          })
        })

crlfe's avatar
crlfe 已提交
70
        this.nextTickBeforeWidth = rect.width!
crlfe's avatar
crlfe 已提交
71 72 73
      });
    },
    nextTickPromise (): void {
crlfe's avatar
crlfe 已提交
74
      this.height = 100
crlfe's avatar
crlfe 已提交
75 76 77 78

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

crlfe's avatar
crlfe 已提交
79 80
		this.height = 200
	  
crlfe's avatar
crlfe 已提交
81 82
        this.$nextTick().then(() => {
          uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
crlfe's avatar
crlfe 已提交
83 84
            const newRect = ret[0] as NodeInfo
            this.nextTickAfterHeight = newRect.height!
crlfe's avatar
crlfe 已提交
85 86 87
          })
        })

crlfe's avatar
crlfe 已提交
88
        this.nextTickBeforeHeight = rect.height!
crlfe's avatar
crlfe 已提交
89
      });
crlfe's avatar
crlfe 已提交
90 91 92 93 94 95 96 97 98 99 100 101
    }
  }
}
</script>

<style>
.row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-bottom: 10px;
}
crlfe's avatar
crlfe 已提交
102 103 104 105

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