提交 625c5890 编写于 作者: 辛宝Otto's avatar 辛宝Otto 🥊

feat: 通过 data 包装的元素不会被侦听包装

上级 f5d577c4
...@@ -24,6 +24,10 @@ ...@@ -24,6 +24,10 @@
<text>obj.arr: </text> <text>obj.arr: </text>
<text id="obj-arr">{{ obj.arr.join(',') }}</text> <text id="obj-arr">{{ obj.arr.join(',') }}</text>
</view> </view>
<view ref='htmlRef' id="idRef" class="flex justify-between flex-row mb-10">
<text>data 存储 element不需要被包装</text>
<text id="isSameRefText">{{ refElementIsSame }}</text>
</view>
<button @click="updateData">update data</button> <button @click="updateData">update data</button>
</view> </view>
</template> </template>
...@@ -34,7 +38,9 @@ ...@@ -34,7 +38,9 @@
num : number, num : number,
arr : number[] arr : number[]
} }
const instance = getCurrentInstance()!.proxy!
const str = ref('default str') const str = ref('default str')
const num = ref(0) const num = ref(0)
// 可通过泛型指定类型 // 可通过泛型指定类型
...@@ -45,17 +51,35 @@ ...@@ -45,17 +51,35 @@
arr: [4, 5, 6] arr: [4, 5, 6]
}) })
const refElement = ref(null)
const refElementIsSame = ref(false)
const refTest = () => {
console.log(instance.proxy);
const queryElementById1 = uni.getElementById('idRef')
const queryElementById2 = uni.getElementById('idRef')
const htmlRefElement = instance.$refs.htmlRef
refElement.value = htmlRefElement
if (queryElementById1 === queryElementById2
&& queryElementById1 === htmlRefElement
&& queryElementById1 === refElement.value
) {
refElementIsSame.value = true
}
}
const updateData = () => { const updateData = () => {
str.value = 'new str' str.value = 'new str'
num.value = 1 num.value = 1
arr.value = [4, 5, 6] arr.value = [4, 5, 6]
obj.value.str = 'new obj.str' obj.value.str = 'new obj.str'
obj.value.num = 100 obj.value.num = 100
obj.value.arr = [7, 8, 9] obj.value.arr = [7, 8, 9]
refTest()
} }
defineExpose({ defineExpose({
updateData updateData
}) })
</script> </script>
\ No newline at end of file
...@@ -24,40 +24,62 @@ ...@@ -24,40 +24,62 @@
<text>obj.arr: </text> <text>obj.arr: </text>
<text id="obj-arr">{{ obj.arr.join(',') }}</text> <text id="obj-arr">{{ obj.arr.join(',') }}</text>
</view> </view>
<view ref='htmlRef' id="idRef" class="flex justify-between flex-row mb-10">
<text>data 存储 element不需要被包装</text>
<text id="isSameRefText">{{ refElementIsSame }}</text>
</view>
<button @click="updateData">update data</button> <button @click="updateData">update data</button>
</view> </view>
</template> </template>
<script lang="uts"> <script lang="uts">
type Obj = { type Obj = {
str : string, str : string,
num : number, num : number,
arr : number[] arr : number[]
} }
export default { export default {
data() { data() {
return { return {
str: 'default str', str: 'default str',
num: 0, num: 0,
arr: [1, 2, 3], arr: [1, 2, 3],
// 特殊类型需要通过 as 指定类型 // 特殊类型需要通过 as 指定类型
obj: { obj: {
str: 'default obj.str', str: 'default obj.str',
num: 10, num: 10,
arr: [4, 5, 6] arr: [4, 5, 6]
} as Obj } as Obj,
} refElement: null,
}, refElementIsSame: false
methods: { }
updateData() { },
this.str = 'new str' methods: {
this.num = 1 refTest() {
this.arr = [4, 5, 6] const queryElementById1 = uni.getElementById('idRef')
const queryElementById2 = uni.getElementById('idRef')
this.obj.str = 'new obj.str' const htmlRefElement = this.$refs.htmlRef
this.obj.num = 100 this.refElement = htmlRefElement
this.obj.arr = [7, 8, 9] if (queryElementById1 === queryElementById2
}, && queryElementById1 === htmlRefElement
}, && queryElementById1 === this.refElement) {
} this.refElementIsSame = true
</script> }
\ No newline at end of file },
updateData() {
this.str = 'new str'
this.num = 1
this.arr = [4, 5, 6]
this.obj.str = 'new obj.str'
this.obj.num = 100
this.obj.arr = [7, 8, 9]
this.refTest()
},
},
}
</script>
...@@ -6,36 +6,39 @@ describe('$data', () => { ...@@ -6,36 +6,39 @@ describe('$data', () => {
const test = async (page) => { const test = async (page) => {
const str = await page.$('#str') const str = await page.$('#str')
expect(await str.text()).toBe('default str') expect(await str.text()).toBe('default str')
const num = await page.$('#num') const num = await page.$('#num')
expect(await num.text()).toBe('0') expect(await num.text()).toBe('0')
const arr = await page.$('#arr') const arr = await page.$('#arr')
expect(await arr.text()).toBe('1,2,3') expect(await arr.text()).toBe('1,2,3')
const objStr = await page.$('#obj-str') const objStr = await page.$('#obj-str')
expect(await objStr.text()).toBe('default obj.str') expect(await objStr.text()).toBe('default obj.str')
const objNum = await page.$('#obj-num') const objNum = await page.$('#obj-num')
expect(await objNum.text()).toBe('10') expect(await objNum.text()).toBe('10')
const objArr = await page.$('#obj-arr') const objArr = await page.$('#obj-arr')
expect(await objArr.text()).toBe('4,5,6') expect(await objArr.text()).toBe('4,5,6')
const elementIsSame = await page.$('#isSameRefText')
expect(await elementIsSame.text()).toBe('false')
await page.callMethod('updateData') await page.callMethod('updateData')
expect(await str.text()).toBe('new str') expect(await str.text()).toBe('new str')
expect(await num.text()).toBe('1') expect(await num.text()).toBe('1')
expect(await arr.text()).toBe('4,5,6') expect(await arr.text()).toBe('4,5,6')
expect(await objStr.text()).toBe('new obj.str') expect(await objStr.text()).toBe('new obj.str')
expect(await objNum.text()).toBe('100') expect(await objNum.text()).toBe('100')
expect(await objArr.text()).toBe('7,8,9') expect(await objArr.text()).toBe('7,8,9')
expect(await elementIsSame.text()).toBe('true')
} }
it('$data 选项式 API', async () => { it('$data 选项式 API', async () => {
page = await program.reLaunch(OPTIONS_PAGE_PATH) page = await program.reLaunch(OPTIONS_PAGE_PATH)
await page.waitFor('view') await page.waitFor('view')
await test(page) await test(page)
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册