child.uvue 2.7 KB
Newer Older
crlfe's avatar
crlfe 已提交
1
<template>
2 3 4 5 6 7
  <view class="page">
    <view class="rect" :style="{width: width + 'px', height: height + 'px'}">child component</view>
    <view class="row"></view>
    <view class="row">
      <text ref="text">$nextTick Before Width:</text>
      <text>{{nextTickBeforeWidth}}</text>
crlfe's avatar
crlfe 已提交
8
    </view>
9 10 11 12 13 14 15 16 17 18 19 20 21
    <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>
  </view>
crlfe's avatar
crlfe 已提交
22 23
</template>

crlfe's avatar
crlfe 已提交
24
<script lang="ts">
25 26 27 28 29 30 31
  export default {
    name: "nextTickChild",
    data() {
      return {
        value: 0,
        width: 100,
        height: 100,
crlfe's avatar
crlfe 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45
        nextTickBeforeWidth: 0,
        nextTickBeforeHeight: 0,
        nextTickAfterWidth: 0,
        nextTickAfterHeight: 0,
      }
    },
    mounted() {
      this.nextTickCallback()
      this.nextTickPromise()
    },
    methods: {
      nextTickCallback() : void {
        this.width = 100
crlfe's avatar
crlfe 已提交
46

47 48 49
        this.$nextTick(() => {
          uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
            const rect = ret[0] as NodeInfo
crlfe's avatar
crlfe 已提交
50

51 52 53 54 55 56 57
            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 已提交
58

59 60 61 62 63 64
            this.nextTickBeforeWidth = rect.width!
          });
        })
      },
      nextTickPromise() : void {
        this.height = 100
crlfe's avatar
crlfe 已提交
65

66 67 68
        this.$nextTick().then(() => {
          uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
            const rect = ret[0] as NodeInfo
crlfe's avatar
crlfe 已提交
69

70 71 72 73 74 75 76
            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!
              })
            })
crlfe's avatar
crlfe 已提交
77

78 79 80 81
            this.nextTickBeforeHeight = rect.height!
          });
        })
      }
crlfe's avatar
crlfe 已提交
82
    }
83
  }
crlfe's avatar
crlfe 已提交
84 85 86
</script>

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

94 95 96 97 98 99 100
  .rect {
    background-color: blue;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>