Erica Du
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    5
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 雙向綁定 ## Vue2 與 Object.defineProperty :::info 在 Vue2.X 版,使用的是 **Object.defineProperty** ::: ```javascript= const app = new Vue({ data: { message: 'Hello', }, }) app.$mount('#app') ``` > Vue 將遍歷所有的 property,並使用 Object.defineProperty 把這些 property 全部轉為 Getter/Setter。Object.defineProperty 是 ES5 的語法,這也是 Vue 不支援 IE8 以及更低版本瀏覽器的原因。 也就是說,Vue 會將所有資料加上 Getter/Setter 屬性,當資料變動時會發出訊息觸發 Watcher,此時 Watcher 就會尋找與資料連動的 DOM,觸發並且重新渲染。 ![](https://i.imgur.com/Pb9Vhtd.png) ### 關於 Object.defineProperty 可參考 JavaScript 核心觀念 - [Object.defineProperty](/t17NCdulRLiG0H2m4QUP_Q) ### 實作 v-model <iframe height="400" style="width: 100%;" scrolling="no" title="Vue2:Make v-model by defineProperty" src="https://codepen.io/ericadu/embed/gOWBXwV?default-tab=js%2Cresult&theme-id=l" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/ericadu/pen/gOWBXwV"> Vue2:Make v-model by defineProperty</a> by Erica (<a href="https://codepen.io/ericadu">@ericadu</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> --- ## 為什麼畫面沒有更新?<font size="4">:zap:</font> Vue2 在實作過程中,經常會發生資料重新賦值,但畫面卻沒有更新的狀況。 可能會發生的這種狀況的情境為: ### **一開始沒有被註冊到的物件不會雙向綁定** Vue2 是透過 `Object.defineProperty` 來實現資料雙向綁定,但如果是在生命週期內才定義物件屬性,因為一開始並沒有透過 `Object.defineProperty` 加上 Setter/Getter,所以這個新增的屬性將不會有雙向綁定的效果。以下提供三種解法: * 一開始就先定義好物件 * 使用官方提供的 `Vue.$set` 語法:`this.$set(object, key, value)` * 透過 `Object.assign()` 方法重新建立一個新的物件讓 Vue 去監控 ### **陣列的長度改變或是利用索引直接賦值時** 在官方文件有舉例兩點無法監聽陣列的狀況: > 利用索引直接設置一個陣列時,例如:vm.items[indexOfItem] = newValue > 修改陣列長度時,例如:vm.items.length = newLength 一般來說資料大多都是為了跑迴圈才使用陣列,但是透過迴圈加入 Setter/Getter 是非常吃效能的,所以通常需要額外處理,因此如果沒有使用 Vue 處理過的語法操作陣列,就會失去雙向綁定的效果。以下提供兩種解法: * 使用 Vue 處理過的語法操作陣列: ``` push()、pop()、shift()、unshift()、splice()、sort()、reverse()、 ``` * 使用 `Vue.$set` --- ## Vue3 與 Proxy :::info 在 Vue3.X 版,使用的是 **Proxy API** ::: ```javascript= const app = Vue.createApp({ data() { return { message: 'Hello', } } }) app.mount('#app') ``` ### Proxy 代理 Proxy 是 ES6 的語法,不同於 `Object.defineProperty`,Proxy 不會直接改變原始資料,而是 **「代理」** 此對象,執行增加/減少的功能。 ``` const p = new Proxy(target, handler) ``` * **target**:可以是任何物件,包含陣列或函式 * **handler**:主要是一個物件,裡面會有許多 target 的操作行為 使用 Proxy 取代 `Object.defineProperty`,除了改善 Vue 的執行效能外,最主要也解決 Vue2 的部分問題: * 無法深層監聽資料: Vue2 是採用迴圈方向去往深層監聽,因此效能上會比較差 * 陣列長度若發生變化,雙向綁定會失效 ### Proxy 結構 Proxy 的結構分為 Handler(控制器) 和 Target(目標)。 用 Handler 來監控 Target 物件的變化,並透過 Getter/Setter 來進行取值或存值。 ```javascript= // 建立 Proxy 控制器 const handler = { get(obj, prop) { return obj[prop] }, set(obj, prop, newVal) { obj[prop] = newVal return true // 告知結束,避免跳錯 }, } // 定義 Proxy 物件,target 一定是物件 const newObj = new Proxy({}, handler) ``` ### Proxy 運作 1. 首先建立渲染函式,並傳入兩個參數分別為: * prop:要渲染的對象 * data:要渲染的資料 ```javascript= const render = (prop, data) => { const target = document.querySelector('#app') target.innerHTML = `${prop}:${data}` } ``` 2. 建立控制器,當每次監聽到資料變動時,控制器會先用 Getter 取值並回傳,然後再透過 Setter 寫入新的值,並且呼叫渲染函式,將資料渲染到畫面上。 ```javascript= const handler = { get(obj, prop) { console.log('get:', obj, prop) return obj[prop] }, set(obj, prop, newVal) { console.log('set:', obj, prop, newVal) obj[prop] = newVal render(prop, obj[prop]) return true }, } ``` 3. 接著要定義 Proxy 物件,這時先用 `console` 看一下 `newObj` 的內容,目前只會印出 Getter 取得的值,還不會進行寫入的動作。 ```javascript= const newObj = new Proxy({message: 'Hi'}, handler) console.log(newObj) ``` ![](https://i.imgur.com/Snb9xCa.png =300x) 4. 最後要透過 Setter 寫入資料,這時才會呼叫渲染函式,並重新渲染到畫面上。 ```javascript= newObj.message = 'Hello' ``` ![](https://i.imgur.com/AmS8nqV.png =300x) ### 實作 v-model <iframe height="400" style="width: 100%;" scrolling="no" title="Vue3:v-model by Proxy" src="https://codepen.io/ericadu/embed/bGWmymy?default-tab=js%2Cresult&theme-id=dark" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/ericadu/pen/bGWmymy"> Vue3:v-model by Proxy</a> by Erica (<a href="https://codepen.io/ericadu">@ericadu</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> --- ## 參考資料 * [JavaScript 核心篇](https://www.hexschool.com/courses/js-core.html) * [深入響應式原理](https://cn.vuejs.org/v2/guide/reactivity.html) * [淺談 Vue2 雙向綁定的概念與實作](https://hsiangfeng.github.io/vue/20210607/665675438/) * [淺談 Vue3 雙向綁定的概念與實作](https://hsiangfeng.github.io/vue/20210627/555169856/) * [為什麼畫面沒有隨資料更新 - PJCHENder](https://pjchender.blogspot.com/2017/05/vue-vue-reactivity.html)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully