Kalan's Blog

Kalan 頭像照片,在淡水拍攝,淺藍背景

四零二曜日電子報上線啦!訂閱訂起來

Software Engineer / Taiwanese / Life in Fukuoka
This blog supports RSS feed (all content), you can click RSS icon or setup through third-party service. If there are special styles such as code syntax in the technical article, it is still recommended to browse to the original website for the best experience.

Current Theme light

標籤:#開發

最近把 Vue 的 Composition API 再重新看一遍,發現除了常用的像是 refreactivecomputed 之外還有好多東西。邊看邊記錄一下自己的想法:

  1. watch: 可以明確定義要監聽的對象。但感覺多數情況下可以被 watchEffect 代替
  2. watchEffect: 只要定義函數即可,Vue 會自動去追蹤裡頭用到的變數然後在有變化時執行。有點像是 useEffect 但不需要開發者自己宣告 dependency array。
  3. reactive ref: reactive 回傳的是一個 proxy,ref 則是一個帶有 value 屬性的物件,比較特別的地方是 value 屬性有 getter 跟 setter。(Vue 實作裡頭定義的,用來偵測值的變化)

我覺得要一直 .value 真的很麻煩,雖然說可以用 $ref,似乎也沒什麼好挑惕的。後來想想如果跟 React 的 useState 比,的確 Vue 的寫法會比較舒服。

ref 還具有雙向綁定的功能,所以可以直接丟進去 v-model 裡面。