劉杰
    • 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

      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.
      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
    • 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 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

    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.
    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Vue.js 文件閱讀 part 2- The Vue Instance --- tags: Javascript relate --- ###### tags: `Javascript, Vue.js` # 創造Vue實體 (Creating a Vue Instance) * 所有的Vue app 都會從創造實例(Instance)開始 * 裡面的options處就是我們操作的地方,可以瀏覽[ API reference](https://vuejs.org/v2/api/#Options-Data)來做操作 ```javascript= var vm = new Vue({ // options }) ``` * 一個Vue app通常會有會有個root Vue instance由new Vue產生 * 伴隨著的是巢狀的樹組成的component,它具有可重複使用的特性 * 在ROOT下面的都是component的部分 ```javascript= Root Instance └─ TodoList ├─ TodoItem │ ├─ TodoButtonDelete │ └─ TodoButtonEdit └─ TodoListFooter ├─ TodosButtonClear └─ TodoListStatistics ``` ## 範例 index.js ```javascript= var app = new Vue({ el:#app, data: { product: "Socks" }) ``` index.html ```htmlembedded= <div id="app"> <h2>{{ product }} </h2> </div> ``` * app使用el連接到html檔案內部的id = "app"的div * 連結app內的data: poduct到html上{{product}} * 藉由改變index.js 內 product的內容可以及時修改html內的{{product}}的內容 呈現在網頁上的結果 ![](https://i.imgur.com/Q0nZBMD.png) 藉著改變屬性也可以及時修改頁面內容 ```javascript= app.product = 'Coat' //印出結果變成Coat app.product = "Compass" //印出結果變成Compass ``` ![](https://i.imgur.com/ZtJ5TBm.png) ![](https://i.imgur.com/tvzMv4N.png) # 資料與方法(Data and Methods) ## $data ```htmlembedded= <div id="app">{{msg}}</div> ``` ```javascript= let data = { msg: '123123' } let vm = new Vue({ el: '#app', data, }); ``` 這兩個是相同的結果 ```javascript= vm.$data === data //true data.msg === vm.msg //true ``` 因此當我們操作 ```javascript= data.msg =456 vm.msg = 123 ``` 畫面也會如此響應 但是如果使用不存在的屬性時畫面則不會響,得使用不同的方法 ```javascript= vm.b = 'hi' ``` 但是如果你知道可能稍後會有屬性會使用到,但目前得空著或是還未存在,因此就必須設預設值 比方說要規畫一個todoList app可能會使用到的data: ```javascript= data: { newTodoText: '', visitCount: 0, hideCompletedTodos: false, todos: [], error: null } ``` ## Object.freeze() 可以關閉reative,讓畫面不被響應 ```htmlembedded= <div id="app">{{msg}}</div> ``` ```javascript= let data = { msg: 'hello Vue' } Object.freeze(data); let vm = new Vue({ el: '#app', data, }); ``` 在這個情況下 ```javascript= data.msg =456 vm.msg = 123 ``` 畫面也不會有任何響應 並vm也會變成只能讀不能寫 ![](https://i.imgur.com/IBZOEP4.png) ## $前綴API $這個符號代表Vue提供給我們的功能 如 $data, $el, $watch等等 [Instance Properties](https://vuejs.org/v2/api/#Instance-Properties) # 生命週期裝置實例(Instance Lifecycle Hooks) 到特定的時間就會呼叫特定的函式 每個Vue實體都會經歷一連串的初始化階段舉例: 1. 需要設置好data observation 1. 編譯模板 1. 把Vue實體推到DOM上 1. 更新DOM當data被更新 在這個階段過程中也會跑一些functions被稱為**Lifecycle Hooks**,讓使用者寫自己的程式碼在特定的階段使用 ## 範例 當Vue實體已經被創造時,created這個函式可以被使用: ```javascript= new Vue({ data: { a: 1 }, created: function () { // `this` points to the vm instance console.log('a is: ' + this.a) } }) // => "a is: 1" ``` ![](https://i.imgur.com/kCZ5wYN.png) 在不同的階段還有其他的hooks會被呼叫例如: 所有的hook指向調用它的實體 * mounted * updated * destroyed ## 作者提醒 千萬不要使用箭頭函式在Vue實體,因為箭頭函示沒有this常常會導致報錯 ``` Uncaught TypeError: Cannot read property of undefined Uncaught TypeError: this.myMethod is not a function ``` 使用一般的函式: ```javascript= new Vue({ data: { a: 1 }, created: function () { console.log(this) } }) ``` 才能印出 Vu實體 ![](https://i.imgur.com/utK0icf.png) 使用箭頭函式: ```javascript= new Vue({ data: { a: 1 }, created: () => { console.log(this) } }) ``` 只會印出外層的windows ![](https://i.imgur.com/yZu3ut0.png) ## Lifecycle Diagram 從上方那張圖可以得知hook如下: **beforeCreate** Vue實體初始化後立刻呼叫此函式,不過此時Vue實體還未創建所以其中的設定都還未能使用(如data observation, event, watcher setup) **created** Vue實例創建完成後立刻呼叫此函式,已設置 data, computed properties, methods, watch/event callbacks,但尚未開始mounting階段,且 $el 還不能在此階段使用。 **beforeMount** 在mounting階段開始前被調用:render function首次被調用。 **mounted** 選項物件中的el被新創建的vm.$el替換,並掛載到到 vm 上,並調用mounted這個鉤子。 **beforeUpdate** 數據被更新時會調用,發生在 Virtual DOM re-render 和 patch 之前(連結:Day4: Virtual DOM),可以在此時更改狀態數據,並不會增加重新渲染的成本。 **updated** 由於數據更新導致 Virtual DOM re-render 和 patch 之後會調用updated這個鉤子。 不精確白話文為:由於updated被調用時,DOM 已經更新。所以在此時更新數據很可能會導致updated無限循環的被調用。 **beforeDestroy** 在 Vue Instance 被銷毀前被調用,因此 Vue Instance 在beforeDestroy中仍可運作。 不精確白話文為:Vue Instance 可以在此時做垂死前的掙扎。 **destroyed** 在 Vue Instance 被銷毀後被調用,此時 Vue Instance 所有東西會解除綁定,事件監聽也都會被移除,子實例也會被銷毀。 # 模板語法(Template Syntax) 未完待續~

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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