Mars 3D
    • 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
    • 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

    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
    --- tags : 六角課程-快速掌握 Vue3.js --- # 六角-一天時間 快速掌握 Vue3.js ## ==Js基本概念== * [this運用](/deD84xTpQaq6BzLBQzl1sw) * [為何要用到 this](/Shjz8mk_R2KNIOE2YLlZpw) ### ==匯入模式 import and export default== * 基礎常見的匯出形式(自己在撰寫模組時最常用) ```javascript= import app from './app_module.js'; ``` * 載入模組時記得 加入 **type="module"** ```javascript= <script type="module"> ``` ### ==起手式架構== * data : 關注點分離,先定義資料 * methods : 方法 (一定是物件) * created : 生命週期 * data 資料本身就是個函式、所以要加入return ```javascript= <script type="module"> import { createApp } from 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.11/vue.esm-browser.js'; // 標準起手式 // 關注點分離,先定義資料 const app = createApp({ data() { return { name: '小明' } }, // 方法列表 methods: { }, // 生命週期 created() { } }); // 不同定義上的概念解說 app.mount('#app') </script> ``` ### ==基礎概念及指令介紹== * 我們用指令的一些運用時機時 以這張表作為參考 ![](https://i.imgur.com/EqB3cOn.png) * :bind 綁定data model裡面的資料常用到 ```htmlembedded= <img :src="綁定資料的 image" alt=""> ``` ### ==Options API== * 在vue 的 生命週期 有兩大重點 * ==created== 資料以建構、但DOM尚未完成。 * ==mounted== 資料以建構、DOM也已完成。 ![](https://i.imgur.com/BciHg4g.png) * 以下面兩個生命週期為例、各取得同一個DOM元素的話, ```javascript= // 生命週期 created() { console.log('created:', document.querySelector('#live')); }, mounted() { console.log('mounted:', document.querySelector('#live')); }, ``` > 只會先出現 mounted 的資料 ![](https://i.imgur.com/KRLi6i9.png) ### ==computed 運算== * computed 可以監聽 ==data==裡面的數值 > 例如data中我們有個數值 為 num:0 ```javascript= data() { return { num: 0 } }, ``` > computed 中我們加入一個函式去做監聽,每當num 有變化時 我們就乘以 2 ```javascript= computed: { num2() { return this.num * 2 } } ``` * 在HTML 中 顯示兩個設定 ```htmlembedded= <input type="number" v-model.number="num"> <div class="h5">原始值:{{ num }}</div> <div class="h5">監聽後:{{ num2 }}</div> ``` > 當監聽後 原始值更動 監聽就會改變 ![](https://i.imgur.com/UpRl0yy.png) ### ==watch 監聽== * 可以處裡比較複雜的函式、可以讀取及寫入data資料 ```javascript= watch: { num() { console.log('watch'); this.getThis(); } }, ``` ### ==元件運用及資料傳遞== * 元件起手式寫法 (全域註冊) * template 與 data 必寫 > 定義元件起手式 寫法 ```javascript= // 全域註冊 const CardComponent = { template: `<div class="card"> <div class="card-body"> {{ title }} </div> </div>`, data() { return { title: '全域元件' } } } ``` ### ==全域註冊== * 定義全域註冊 : 必須在 createApp 的後方 ```javascript= const app = createApp({ }); // 定義全域註冊 : 必須在 createApp 的後方 app.component('card',CardComponent); ``` * component 定義方式 * 第一個為自取名稱、第二個是定義元件宣告變數 ```javascript= app.component('card',CardComponent); ``` * 元件上一律小駝峰命名 cardComponent * 也因為HTML標籤無法大小寫命名 所以元件標籤會分開寫類似 ```htmlembedded= <card-component></card-component> ``` ### ==區域註冊== * 在區域註冊中使用 components 可定義許多元件 ```javascript= const app = createApp({ components: { card, CardComponent2: { template: `<div class="card"> <div class="card-body"> {{ title }} </div> </div>`, data() { return { title: '區域元件' } } }, CardComponent3: { template: `<div class="card"> <div class="card-body"> {{ title }} <card-component></card-component> </div> </div>`, data() { return { title: '區域元件 3' } } } } }); ``` ### ==props 與 emit 資料傳遞== * 在資料傳遞之間內外層用到的 props (值) 與 emit(事件) * props (值) - 由外層傳到內層 * emit (事件) - 由內層傳到外層 ![](https://i.imgur.com/uvqSU7E.png) ### ==props (傳值)== * 以順序來說 先在內層定義 ==props== (接收值的方式) * 內層要接受的值 ,值可以自己定義 > 內層元件 定義 props 接收的變數 及顯示 {{outer}} ```javascript= //內層元件 const cardComponent = { template: `<div class="card"> <div class="card-body"> {{ title }} {{outer}} </div> </div>`, data() { return { title: '全域元件' } }, //內層要接受的值 變數可以自己定義 props: ['outer'] } ``` > 外層元件 傳值進去 預計將 data中的 title 傳遞進去 內層 ```javascript= //外層元件 const app = createApp({ data() { return { title: '外層元件' } }, components: { cardComponent } }); ``` > 顯示綁定 ==前內後外 心法== outer是內層定義的變數、title是外層傳進去的值 ```htmlembedded= <card-component :outer="title"> </card-component> ``` ### ==emit (事件觸發)== * 由內層元件而外傳遞外層元件 * 需在內層元件加上一個 methods: * 觸發事件需加上$emit、(第一個自訂的名稱,第二個向外傳出的值) ```javascript= const cardComponent = { template: `<div class="card"> <div class="card-body"> {{ title }} , {{outer}} </div> // 觸發事件 <button type="button" @click="pushData">emit </button> </div>`, data() { return { title: '全域元件' } }, //需在內層元件加上一個 methods: methods: { pushData() { //(第一個自訂的名稱,第二個向外傳出的值) this.$emit('push-data',this.title) } }, props: ['outer'] } ``` * 外層元件 也需要定義一個methods方法 * 並在標籤元件上定義一個觸發器 * 觸發器 是前內後外命名 ```javascript= // 外層元件 const app = createApp({ data() { return { title: '外層元件' } }, components: { cardComponent }, //外層元件 也需要定義一個methods方法接收 methods: { getData(item) { console.log(item); } }, }); ``` > 並在標籤元件上定義一個 觸發器 使用 "@" ```htmlembedded= <card-component :outer="title" <!-- @觸發器 是前內後外命名 內層emit的命名="外層函式方法" --> @push-data='getData'> </card-component> ``` ### ==ref 取得動元素== * ref類似在抓取Dom元素的節點上 * 需要在Dom元件產生生命週期時,用 mounted 才能開始用 ref 取得Dom元素 * ref後面為自訂名稱 ref="自訂名稱" * 在mounted使用 vue的 refs + 自訂名稱 取得動元素 > ref 後面為自訂名稱 ref="自訂名稱" ```htmlembedded= <button ref="buttons" type="button">點我</button> ``` > 在mounted使用 vue的 refs + 自訂名稱 取得動元素 ```javascript= mounted() { console.log(this.$refs.buttons); }, ``` > ref的概念 ![](https://i.imgur.com/wfMkHcO.png) ### ==Proxy 與 Composition API== ### Proxy 重點: * Proxy 是由 物件、Handler 組成 * 透過 Handler 的 get, set 監聽,可以做到雙向綁定的特性 * Proxy 傳入必定是物件 * 預設只有一層 ### ==ref , reactive 選擇要點== * reactive 一定要是物件 * reactive 不可重新被賦值 * 怕錯,就都用 ref (只需要在後面加上 value加以運用) 例: obj2.value

    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