Skip to content

Commit 6509a3e

Browse files
committed
chore: update readme
1 parent 1cf90f3 commit 6509a3e

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

README-zh_TW.md

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[English](./README.md) | 繁體中文
1+
[English](./README.md) | 中文
22

33
# vue-condition-watcher <img src="https://slackmojis.com/emojis/43271-glasses/download" width="40" />
44

@@ -20,6 +20,7 @@
2020
✔ 輕鬆處理分頁的需求,簡單客製自己的分頁邏輯<br/>
2121
✔ 當網頁重新聚焦或是網絡斷線恢復自動重新請求資料<br/>
2222
✔ 支援輪詢,可動態調整輪詢週期<br/>
23+
✔ 緩存機制讓資料可以更快呈現,不用再等待 loading 動畫<br/>
2324
✔ 不需要等待回傳結果,可手動改變 `data` 讓使用者體驗更好<br/>
2425
✔ 支援 TypeScript<br/>
2526
✔ 支援 Vue 2 & 3,感謝 [vue-demi](https://github.com/vueuse/vue-demi)
@@ -47,6 +48,7 @@
4748
- [Changelog](https://github.com/runkids/vue-condition-watcher/blob/master/CHANGELOG.md)
4849

4950
## Demo
51+
5052
[👉 (推薦) 這邊下載 Vue3 版本範例](https://github.com/runkids/vue-condition-watcher/tree/master/examples/vue3) (使用 [Vite](https://github.com/vuejs/vite))
5153

5254
```bash
@@ -65,7 +67,7 @@ yarn serve
6567
6668
### 👉 線上 Demo
6769
68-
[Go to stackblitz](https://stackblitz.com/edit/vitejs-vite-tsvfqu?devtoolsheight=33&embed=1&file=src/views/Home.vue)
70+
- [Demo with Vue 3 on StackBlitz](https://stackblitz.com/edit/vitejs-vite-tsvfqu?devtoolsheight=33&embed=1&file=src/views/Home.vue)
6971
7072
## 入門
7173
@@ -142,6 +144,7 @@ const { conditions, data, error, loading, execute, resetConditions, onConditions
142144
```
143145

144146
### Configs
147+
145148
- `fetcher`: (⚠️ 必要) 請求資料的 promise function。
146149
- `conditions`: (⚠️ 必要) `conditions` 預設值。
147150
- `defaultParams`: 每次請求預設會帶上的參數,不可修改。
@@ -278,10 +281,10 @@ execute()
278281
### 攔截請求
279282

280283
`beforeFetch` 可以讓你在請求之前再次修改 `conditions`
281-
* 第一個參數回傳一個深拷貝的 `conditions`,你可以任意的修改它且不會影響原本 `conditions`,你可以在這邊調整要給後端的 API 格式。
282-
* 第二個參數回傳一個 function,執行它將會終止這次請求。這在某些情況會很有用的。
283-
* `beforeFetch` 可以處理同步與非同步行為。
284-
* 必須返回修改後的 `conditions`
284+
- 第一個參數回傳一個深拷貝的 `conditions`,你可以任意的修改它且不會影響原本 `conditions`,你可以在這邊調整要給後端的 API 格式。
285+
- 第二個參數回傳一個 function,執行它將會終止這次請求。這在某些情況會很有用的。
286+
- `beforeFetch` 可以處理同步與非同步行為。
287+
- 必須返回修改後的 `conditions`
285288

286289
```js
287290
useConditionWatcher({
@@ -307,8 +310,8 @@ useConditionWatcher({
307310
```
308311

309312
`afterFetch` 可以在更新 `data` 前攔截請求,這時候的 `loading` 狀態還是 `true`
310-
* 你可以在這邊做依賴請求 🎭,或是處理其他同步與非同步行為
311-
* 可以在這邊最後修改 `data`,返回的值將會是 `data` 的值
313+
- 你可以在這邊做依賴請求 🎭,或是處理其他同步與非同步行為
314+
- 可以在這邊最後修改 `data`,返回的值將會是 `data` 的值
312315

313316
```js
314317
const { data } = useConditionWatcher({
@@ -331,8 +334,9 @@ console.log(data) //[{message: 'Hello', sender: 'runkids'}]
331334
```
332335

333336
`onFetchError` 可以攔截錯誤,可以在 `data``error` 更新前調整 `error` & `data`,這時候的 `loading` 狀態還是 `true`
334-
* `onFetchError` 可以處理同步與非同步行為。
335-
* 最後返回格式必須為
337+
- `onFetchError` 可以處理同步與非同步行為。
338+
- 最後返回格式必須為
339+
336340
```js
337341
{
338342
data: ... ,
@@ -361,17 +365,21 @@ console.log(error) //'Error Message'
361365
```
362366

363367
### 變異資料
368+
364369
在一些情況下, mutations `data` 是提升用戶體驗的好方法,因為不需要等待 API 回傳結果。
365370

366371
使用 `mutate` function, 你可以修改 `data`。 當 `onFetchSuccess` 觸發時會再改變 `data`
367372

368373
有兩種方式使用 `mutate` function:
369374

370375
- 第一種:完整修改 data.
376+
371377
```js
372378
mutate(newData)
373379
```
380+
374381
- 第二種:使用 callback function,會接受一個深拷貝的 `data` 資料,修改完後再返回結果
382+
375383
```js
376384
const finalData = mutate((currentData) => {
377385
currentData[0].name = 'runkids'
@@ -380,8 +388,11 @@ const finalData = mutate((currentData) => {
380388

381389
console.log(finalData[0]name === data.value[0].name) //true
382390
```
391+
383392
#### 🏄‍♂️ 範例:依據目前的資料來修改部分資料
393+
384394
POST API 會返回更新後的結果,我們不需要重新執行 `execute` 更新結果。我們可以用 `mutate` 的第二種方式來修改部分改動。
395+
385396
```js
386397
const { conditions, data, mutate } = useConditionWatcher({
387398
fetcher: api.userInfo,
@@ -447,7 +458,6 @@ onFetchFinally(() => {
447458
})
448459
```
449460

450-
451461
## 輪詢
452462

453463
你可以透過設定 `pollingInterval` 啟用輪詢功能(當為 0 時會關閉此功能)
@@ -459,6 +469,7 @@ useConditionWatcher({
459469
pollingInterval: 1000
460470
})
461471
```
472+
462473
你還可以使用 `ref` 動態響應輪詢週期。
463474

464475
```js
@@ -498,12 +509,15 @@ useConditionWatcher({
498509
revalidateOnFocus: true // revalidateOnFocus default is false
499510
})
500511
```
512+
501513
## 緩存
502514

503515
`vue-condition-watcher` 預設會在當前組件緩存你的第一次數據。接著後面的請求會先使用緩存數據,背後默默請求新資料,等待最新回傳結果並比對緩存資料是否相同,達到類似預加載的效果。
504516

505517
你也可以設定 `cacheProvider` 全局共用或是緩存資料在 `localStorage`,搭配輪詢可以達到分頁同步資料的效果。
518+
506519
###### Global Based
520+
507521
```js
508522
// App.vue
509523
<script lang="ts">
@@ -524,7 +538,9 @@ useConditionWatcher({
524538
})
525539
</script>
526540
```
541+
527542
###### [LocalStorage Based](https://swr.vercel.app/docs/advanced/cache#localstorage-based-persistent-cache)
543+
528544
```js
529545
function localStorageProvider() {
530546
const map = new Map(JSON.parse(localStorage.getItem('your-cache-key') || '[]'))
@@ -543,6 +559,7 @@ useConditionWatcher({
543559
```
544560

545561
## History 模式
562+
546563
你可以設定 `config.history` 啟用 History 模式,是基於 vue-router 的,支援 v3 和 v4 版本
547564

548565
```js
@@ -558,6 +575,7 @@ useConditionWatcher({
558575
```
559576

560577
你還可以設定 `history.ignore` 排除 `conditions` 部分的 `key&value` 不要同步到 URL query string.
578+
561579
```js
562580
const router = useRouter()
563581

@@ -578,6 +596,7 @@ useConditionWatcher({
578596
```
579597

580598
History mode 會轉換 `conditions`預設值的對應型別到 query string 而且會過濾掉 `undefined`, `null`, `''`, `[]` 這些類型的值.
599+
581600
```js
582601
conditions: {
583602
users: ['runkids', 'hello']
@@ -589,10 +608,13 @@ conditions: {
589608
```
590609

591610
每當你重新整理網頁還會自動同步 query string 到 `conditions`
611+
592612
```
593613
URL query string: ?offset=0&limit=10&users=runkids,hello&company=vue
594614
```
615+
595616
`conditions` 將變成
617+
596618
```js
597619
{
598620
users: ['runkids', 'hello']
@@ -601,6 +623,7 @@ URL query string: ?offset=0&limit=10&users=runkids,hello&company=vue
601623
offset: 0
602624
}
603625
```
626+
604627
## 生命週期
605628

606629
<img src=".github/vue-condition-watcher_lifecycle.jpeg"/>
@@ -897,18 +920,21 @@ function usePagination () {
897920
```
898921

899922
當 daterange or limit 改變時, 會將 offset 設置為 0,接著才會重新觸發請求。
923+
900924
## TDOD List
901925

902926
- [ ] Cache
903927
- [ ] Prefetching
904928
- [ ] Automatic Revalidation
905929
- [ ] Error Retry
906930
- [ ] Nuxt SSR SSG Support
931+
907932
## Thanks
908933

909934
This project is heavily inspired by the following awesome projects.
910935

911936
- [vercel/swr](https://github.com/vercel/swr)
937+
912938
## 📄 License
913939

914-
[MIT License](https://github.com/runkids/vue-condition-watcher/blob/master/LICENSE) © 2020-PRESENT [Runkids](https://github.com/runkids)
940+
[MIT License](https://github.com/runkids/vue-condition-watcher/blob/master/LICENSE) © 2020-PRESENT [Runkids](https://github.com/runkids)

0 commit comments

Comments
 (0)