From 7f1845d5e22f58154a33e8597a39c9a2bfc17490 Mon Sep 17 00:00:00 2001
From: zhenyuWang <13641039885@163.com>
Date: Sat, 20 Apr 2024 17:41:36 +0800
Subject: [PATCH] refactor(reactivity): computed
---
pages.json | 10 ++-
pages/index/index.uvue | 68 ++++++++++++++++---
.../core/computed/computed-composition.uvue | 47 +++++++++++++
.../core/computed/computed-options.uvue | 52 ++++++++++++++
.../reactivity/core/computed/computed.test.js | 41 +++++++----
pages/reactivity/core/computed/computed.uvue | 35 ----------
6 files changed, 191 insertions(+), 62 deletions(-)
create mode 100644 pages/reactivity/core/computed/computed-composition.uvue
create mode 100644 pages/reactivity/core/computed/computed-options.uvue
delete mode 100644 pages/reactivity/core/computed/computed.uvue
diff --git a/pages.json b/pages.json
index 7e6eddc..98b2b59 100644
--- a/pages.json
+++ b/pages.json
@@ -468,9 +468,15 @@
}
},
{
- "path": "pages/reactivity/core/computed/computed",
+ "path": "pages/reactivity/core/computed/computed-options",
"style": {
- "navigationBarTitleText": "computed"
+ "navigationBarTitleText": "computed 选项式 API"
+ }
+ },
+ {
+ "path": "pages/reactivity/core/computed/computed-composition",
+ "style": {
+ "navigationBarTitleText": "computed 组合式 API"
}
},
{
diff --git a/pages/index/index.uvue b/pages/index/index.uvue
index b27e5d8..b47eb25 100644
--- a/pages/index/index.uvue
+++ b/pages/index/index.uvue
@@ -26,15 +26,45 @@
@@ -434,7 +464,23 @@ export default {
id: 'ref',
name: 'ref',
url: 'ref/ref'
- }
+ },
+ {
+ id: 'computed',
+ name: 'computed',
+ children: [
+ {
+ id: 'computed-options',
+ name: 'computed 选项式 API',
+ url: 'computed-options'
+ },
+ {
+ id: 'computed-composition',
+ name: 'computed 组合式 API',
+ url: 'computed-composition'
+ },
+ ]
+ },
]
}
] as Page[]
@@ -635,6 +681,8 @@ export default {
})
return
}
+console.log('parentUrl', parentUrl)
+console.log('page', page)
uni.navigateTo({
url: `/pages/${parentUrl}/${page.url}`
})
diff --git a/pages/reactivity/core/computed/computed-composition.uvue b/pages/reactivity/core/computed/computed-composition.uvue
new file mode 100644
index 0000000..1147686
--- /dev/null
+++ b/pages/reactivity/core/computed/computed-composition.uvue
@@ -0,0 +1,47 @@
+
+
+
+ count:
+ {{ count }}
+
+
+ computed double count:
+ {{ doubleCount }}
+
+
+ obj.arr:
+ {{ JSON.stringify(obj.arr) }}
+
+
+ computed obj.arr.length:
+ {{ objArrLen }}
+
+
+
+
+
+
diff --git a/pages/reactivity/core/computed/computed-options.uvue b/pages/reactivity/core/computed/computed-options.uvue
new file mode 100644
index 0000000..9869581
--- /dev/null
+++ b/pages/reactivity/core/computed/computed-options.uvue
@@ -0,0 +1,52 @@
+
+
+
+ count:
+ {{ count }}
+
+
+ computed double count:
+ {{ doubleCount }}
+
+
+ obj.arr:
+ {{ JSON.stringify(obj.arr) }}
+
+
+ computed obj.arr.length:
+ {{ objArrLen }}
+
+
+
+
+
+
diff --git a/pages/reactivity/core/computed/computed.test.js b/pages/reactivity/core/computed/computed.test.js
index 6bed895..a4d02f0 100644
--- a/pages/reactivity/core/computed/computed.test.js
+++ b/pages/reactivity/core/computed/computed.test.js
@@ -1,29 +1,40 @@
-const PAGE_PATH = '/pages/composition-api/reactivity/computed/computed'
+const OPTIONS_PAGE_PATH = '/pages/reactivity/core/computed/computed-options'
+const COMPOSITION_PAGE_PATH = '/pages/reactivity/core/computed/computed-composition'
describe('computed', () => {
let page = null
- beforeAll(async () => {
- page = await program.reLaunch(PAGE_PATH)
- await page.waitFor('view')
- })
- it('basic', async () => {
+
+ const test = async (page) => {
const count = await page.$('#count')
- expect(await count.text()).toBe('count: 0')
+ expect(await count.text()).toBe('0')
const doubleCount = await page.$('#double-count')
- expect(await doubleCount.text()).toBe('computed double count: 0')
+ expect(await doubleCount.text()).toBe('0')
const objArr = await page.$('#obj-arr')
- expect(await objArr.text()).toBe('obj.arr: [1,2,3]')
+ expect(await objArr.text()).toBe('[1,2,3]')
const objArrLen = await page.$('#obj-arr-len')
- expect(await objArrLen.text()).toBe('computed obj.arr.length: 3')
+ expect(await objArrLen.text()).toBe('3')
const updateBtn = await page.$('#update-btn')
await updateBtn.tap()
- expect(await count.text()).toBe('count: 1')
- expect(await doubleCount.text()).toBe('computed double count: 2')
- expect(await objArr.text()).toBe('obj.arr: [1,2,3,4]')
- expect(await objArrLen.text()).toBe('computed obj.arr.length: 4')
+ expect(await count.text()).toBe('1')
+ expect(await doubleCount.text()).toBe('2')
+ expect(await objArr.text()).toBe('[1,2,3,4]')
+ expect(await objArrLen.text()).toBe('4')
+ }
+
+ it('computed options API', async () => {
+ page = await program.reLaunch(OPTIONS_PAGE_PATH)
+ await page.waitFor('view')
+
+ await test(page)
+ })
+
+ it('computed composition API', async () => {
+ page = await program.reLaunch(COMPOSITION_PAGE_PATH)
+ await page.waitFor('view')
+
+ await test(page)
})
-
})
\ No newline at end of file
diff --git a/pages/reactivity/core/computed/computed.uvue b/pages/reactivity/core/computed/computed.uvue
deleted file mode 100644
index 62f0dd3..0000000
--- a/pages/reactivity/core/computed/computed.uvue
+++ /dev/null
@@ -1,35 +0,0 @@
-
-
- count: {{ count }}
- computed double count: {{ doubleCount }}
- obj.arr: {{ JSON.stringify(obj.arr) }}
- computed obj.arr.length: {{ objArrLen }}
-
-
-
-
-
\ No newline at end of file
--
GitLab