提交 56b9ae3f 编写于 作者: 雪洛's avatar 雪洛

fix: $nextTick测试例createSelectorQuery应使用in

上级 62271135
<template> <template>
<view class="page"> <view class="page">
<view class="rect" :style="{width: width, height: height}">child component</view> <view class="rect" :style="{width: width + 'px', height: height + 'px'}">child component</view>
<view class="row"></view> <view class="row"></view>
<view class="row"> <view class="row">
<text ref="text">$nextTick Before Width:</text> <text ref="text">$nextTick Before Width:</text>
<text>{{nextTickBeforeWidth}}</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>
</view> </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>
</view>
</template> </template>
<script lang="ts"> <script lang="ts">
export default { export default {
name: "nextTickChild", name: "nextTickChild",
data() { data() {
return { return {
value: 0, value: 0,
width: 100, width: 100,
height: 100, height: 100,
nextTickBeforeWidth: 0, nextTickBeforeWidth: 0,
nextTickBeforeHeight: 0, nextTickBeforeHeight: 0,
nextTickAfterWidth: 0, nextTickAfterWidth: 0,
nextTickAfterHeight: 0, nextTickAfterHeight: 0,
} }
}, },
mounted() { mounted() {
this.nextTickCallback() this.nextTickCallback()
this.nextTickPromise() this.nextTickPromise()
}, },
methods: { methods: {
nextTickCallback(): void { nextTickCallback() : void {
this.width = 100 this.width = 100
this.$nextTick(() => { this.$nextTick(() => {
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => { uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
const rect = ret[0] as NodeInfo const rect = ret[0] as NodeInfo
this.width = 200 this.width = 200
this.$nextTick(() => { this.$nextTick(() => {
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => { uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
const newRect = ret[0] as NodeInfo const newRect = ret[0] as NodeInfo
this.nextTickAfterWidth = newRect.width! this.nextTickAfterWidth = newRect.width!
}) })
}) })
this.nextTickBeforeWidth = rect.width! this.nextTickBeforeWidth = rect.width!
}); });
}) })
}, },
nextTickPromise(): void { nextTickPromise() : void {
this.height = 100 this.height = 100
this.$nextTick().then(() => { this.$nextTick().then(() => {
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => { uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
const rect = ret[0] as NodeInfo const rect = ret[0] as NodeInfo
this.height = 200 this.height = 200
this.$nextTick().then(() => { this.$nextTick().then(() => {
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => { uni.createSelectorQuery().in(this).select('.rect').boundingClientRect(null).exec((ret : Array<any>) => {
const newRect = ret[0] as NodeInfo const newRect = ret[0] as NodeInfo
this.nextTickAfterHeight = newRect.height! this.nextTickAfterHeight = newRect.height!
}) })
}) })
this.nextTickBeforeHeight = rect.height! this.nextTickBeforeHeight = rect.height!
}); });
}) })
} }
}
} }
}
</script> </script>
<style> <style>
.row { .row {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
margin-bottom: 10px; margin-bottom: 10px;
} }
.rect { .rect {
background-color: blue; background-color: blue;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
} }
</style> </style>
\ No newline at end of file
<template> <template>
<!-- #ifdef APP --> <!-- #ifdef APP -->
<scroll-view style="flex: 1"> <scroll-view style="flex: 1">
<!-- #endif --> <!-- #endif -->
<view class="page"> <view class="page">
<view class="rect" :style="{ width: width, height: height }"></view> <view class="rect" :style="{ width: width + 'px', height: height + 'px' }"></view>
<view class="row"></view> <view class="row"></view>
<view class="row"> <view class="row">
<text ref="text">$nextTick Before Width:</text> <text ref="text">$nextTick Before Width:</text>
...@@ -23,84 +23,76 @@ ...@@ -23,84 +23,76 @@
</view> </view>
<next-tick-child class="next-tick-child"></next-tick-child> <next-tick-child class="next-tick-child"></next-tick-child>
</view> </view>
<!-- #ifdef APP --> <!-- #ifdef APP -->
</scroll-view> </scroll-view>
<!-- #endif --> <!-- #endif -->
</template> </template>
<script lang="ts"> <script lang="ts">
import child from './child.uvue' import child from './child.uvue'
export default {
components: {
nextTickChild: child
},
data () {
return {
value: 0,
width: 100,
height: 100,
nextTickBeforeWidth: 0,
nextTickBeforeHeight: 0,
nextTickAfterWidth: 0,
nextTickAfterHeight: 0,
}
},
mounted () {
this.nextTickCallback()
this.nextTickPromise()
},
methods: {
nextTickCallback (): void {
this.width = 100
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
const rect = ret[0] as NodeInfo
this.width = 200
this.$nextTick(() => {
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => {
const newRect = ret[0] as NodeInfo
this.nextTickAfterWidth = newRect.width!
})
})
this.nextTickBeforeWidth = rect.width! export default {
}); components: {
nextTickChild: child
}, },
nextTickPromise (): void { data() {
this.height = 100 return {
value: 0,
width: 100,
height: 100,
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => { nextTickBeforeWidth: 0,
const rect = ret[0] as NodeInfo nextTickBeforeHeight: 0,
nextTickAfterWidth: 0,
this.height = 200 nextTickAfterHeight: 0,
}
this.$nextTick().then(() => { },
uni.createSelectorQuery().select('.rect').boundingClientRect(null).exec((ret: Array<any>) => { mounted() {
const newRect = ret[0] as NodeInfo this.nextTickCallback()
this.nextTickAfterHeight = newRect.height! 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!
})
}) })
}) this.nextTickBeforeWidth = rect.width!
});
this.nextTickBeforeHeight = rect.height! },
}); 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!
});
}
} }
} }
}
</script> </script>
<style> <style>
.row { .row {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
margin-bottom: 10px; margin-bottom: 10px;
} }
.rect { .rect {
background-color: red; background-color: red;
} }
</style> </style>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册