提交 9941fe78 编写于 作者: H hdx

watch: 补充立即执行例子

上级 71dd587f
...@@ -12,20 +12,27 @@ ...@@ -12,20 +12,27 @@
<text>子组件 值是否变化</text> <text>子组件 值是否变化</text>
<text>{{ changed }}</text> <text>{{ changed }}</text>
</view> </view>
<view class="row">
<text>immediate=true 值应为 true</text>
<text>{{ immediateChanged }}</text>
</view>
</view> </view>
</template> </template>
<script> <script>
export default { export default {
name: "watch-child", name: "watch-child",
data() { data() {
return { return {
val: "init", val: "init",
changed: false, changed: false,
immediateValue: 'value',
immediateChanged: false,
} }
}, },
mounted() { mounted() {
this.createWatch() this.createWatch()
this.createImmediateWatch()
}, },
methods: { methods: {
createWatch() { createWatch() {
...@@ -34,16 +41,23 @@ export default { ...@@ -34,16 +41,23 @@ export default {
}) })
this.val = 'changed' this.val = 'changed'
},
createImmediateWatch() {
this.$watch('immediateWatchValue', () => {
this.immediateChanged = !this.immediateChanged
}, {
immediate: true
})
},
} }
} }
}
</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;
} }
</style> </style>
\ No newline at end of file
...@@ -14,6 +14,9 @@ describe('$watch()', () => { ...@@ -14,6 +14,9 @@ describe('$watch()', () => {
expect(value).not.toBe(initValue) expect(value).not.toBe(initValue)
expect(isChange).toBe(true) expect(isChange).toBe(true)
const isChange2 = await (await page.data()).immediateChanged
expect(isChange2).toBe(true)
}) })
it('子组件 $watch() 生效', async () => { it('子组件 $watch() 生效', async () => {
...@@ -24,5 +27,8 @@ describe('$watch()', () => { ...@@ -24,5 +27,8 @@ describe('$watch()', () => {
expect(value).not.toBe(initValue) expect(value).not.toBe(initValue)
expect(isChange).toBe(true) expect(isChange).toBe(true)
const isChange2 = await (await comp.data()).immediateChanged
expect(isChange2).toBe(true)
}) })
}) })
\ No newline at end of file
...@@ -12,14 +12,18 @@ ...@@ -12,14 +12,18 @@
<text>值是否变化</text> <text>值是否变化</text>
<text>{{ changed }}</text> <text>{{ changed }}</text>
</view> </view>
<view class="row">
<text>immediate=true 值应为 true</text>
<text>{{ immediateChanged }}</text>
</view>
<watch-child class="watch-child"></watch-child> <watch-child class="watch-child"></watch-child>
</view> </view>
</template> </template>
<script> <script>
import child from './child.uvue' import child from './child.uvue'
export default { export default {
components: { components: {
watchChild: child watchChild: child
}, },
...@@ -27,6 +31,8 @@ export default { ...@@ -27,6 +31,8 @@ export default {
return { return {
val: "init", val: "init",
changed: false, changed: false,
immediateValue: 'value',
immediateChanged: false,
// a: 'a', // a: 'a',
// b: 'b', // b: 'b',
// abChanged: false // abChanged: false
...@@ -34,6 +40,7 @@ export default { ...@@ -34,6 +40,7 @@ export default {
}, },
mounted() { mounted() {
this.createWatch() this.createWatch()
this.createImmediateWatch()
// this.createGetterWatch() // this.createGetterWatch()
}, },
methods: { methods: {
...@@ -44,6 +51,13 @@ export default { ...@@ -44,6 +51,13 @@ export default {
this.val = 'changed' this.val = 'changed'
}, },
createImmediateWatch() {
this.$watch('immediateWatchValue', () => {
this.immediateChanged = !this.immediateChanged
}, {
immediate: true
})
},
// createGetterWatch () { // createGetterWatch () {
// this.$watch(() => this.a + this.b, () => { // this.$watch(() => this.a + this.b, () => {
// this.abChanged = !this.abChanged // this.abChanged = !this.abChanged
...@@ -52,14 +66,14 @@ export default { ...@@ -52,14 +66,14 @@ export default {
// this.a = 'changed' // this.a = 'changed'
// } // }
} }
} }
</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;
} }
</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.
先完成此消息的编辑!
想要评论请 注册