助教天團
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 🏅 Day4 - Vue.js 的基礎魔法:v-for ## v-for `v-for` 可以針對一組陣列或物件進行渲染,指令中會使用 `(item, key) in array` 的語法,其中: - item:陣列迭代的元素別名,名稱可自訂 - key:如果是陣列則為該迭代的索引位置,如果是物件則為該迭代的物件屬性 - array:陣列或資料的來源 範例為陣列、物件搭配迴圈的方式,範例重點: - v-for 的結構為 `v-for="(item, key) in array"` - 無論是陣列、物件都可以使用 v-for - 陣列的索引為 `0, 1, 2...`,物件索引則為屬性名稱 <iframe height="300" style="width: 100%;" scrolling="no" title="🏅 Day4 - Vue.js 的基礎魔法:v-for 題目" src="https://codepen.io/danny666/embed/oNroYrE?default-tab=js%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/danny666/pen/oNroYrE"> 🏅 Day4 - Vue.js 的基礎魔法:v-for 題目</a> by Rain (<a href="https://codepen.io/danny666">@danny666</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> ## v-for 注意事項 由於 v-for 在運作上是採用快速替換的形式。因此有部分元素會沒有完整的被替換, 當 input 輸入內容後,按下反轉陣列時: - 如果沒有 key 時,則 input 位置不會被同時更動 - 當有加上 key 時,input 位置會與原本的資料內容位置一起變動 **新版的 Vue 相關開發工具中,都會強烈建議加上 `key`。** 範例重點: - 範例最下方有一個反轉陣列的按鈕,按下後會反轉上方的結構 - 試著輸入一些內容,並反轉整個結構 - 注意元素中是否有 key 屬性,這會影響到是否能夠 input 是否有隨著結構反轉 <iframe height="300" style="width: 100%;" scrolling="no" title="Composition API:v-for 範例 (2)" src="https://codepen.io/yen-kg/embed/LYodGrL?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/yen-kg/pen/LYodGrL"> Composition API:v-for 範例 (2)</a> by Yen Kg (<a href="https://codepen.io/yen-kg">@yen-kg</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> ## Template 當渲染陣列資料卻不便產生新的標籤時,可以搭配 `<template>` 標籤做使用,此方法即可在產生 DOM 結構時不產生額外的標籤。另外 `template` 標籤也同樣可用於 `v-for` 上 範例重點: - 可以使用 `template` 標籤替代原有的 HTML 標籤,而 `template` 標籤是不會被輸出的。 <iframe height="300" style="width: 100%;" scrolling="no" title="Composition API:v-for 範例 (3)" src="https://codepen.io/yen-kg/embed/zYQWryo?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/yen-kg/pen/zYQWryo"> Composition API:v-for 範例 (3)</a> by Yen Kg (<a href="https://codepen.io/yen-kg">@yen-kg</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> ## 避免 v-if 和 v-for 用在一起 參考資料:https://cn.vuejs.org/guide/essentials/list.html#v-for-with-v-if 為了避免不要的錯誤,Vue.js 的規範中建議不要將 v-for 與 v-if 混合使用。搭配進階工具如 Vue Cli 及 ESLint 時,兩者混合使用會跳出錯誤。 範例重點: - 盡可能將 v-if 與 v-for 用不同的標籤呈現 <iframe height="300" style="width: 100%;" scrolling="no" title="Composition API:v-for 範例 (4)" src="https://codepen.io/yen-kg/embed/pomLygy?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/yen-kg/pen/pomLygy"> Composition API:v-for 範例 (4)</a> by Yen Kg (<a href="https://codepen.io/yen-kg">@yen-kg</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> 題目 --- 操作 [這個模板](https://codepen.io/yen-kg/pen/zYQWqWR),執行以下要求(只能操作 HTML 的部分): **請使用 v-for 渲染資料如下圖:** ![image](https://hackmd.io/_uploads/BypODUwUC.png) ## 回報流程 將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/vftL5i0.png) <!-- 解答: <template> <div id="app" class="container"> <div class="row"> <div v-for="(fruit, index) in fruitData" :key="index" class="col-md-6 mb-3"> <div class="card" style="width: 100%;"> <div class="card-body"> <h5 class="card-title">水果名稱:{{ fruit.title }}</h5> <p class="card-text my-2">價錢:{{ fruit.price }}</p> <p class="card-text mb-3">數量:{{ fruit.count }}</p> <a href="#" class="btn btn-primary">加入購物車</a> </div> </div> </div> </div> </div> </template> --> 回報區 --- | # | Discord | CodePen / 答案 | |:---:|:---------- |:------------------------------------------------------------ | | 01 | 登登登 | [CodePen](https://codepen.io/Duncanin/pen/vENmobM) | | 02 | Gui | [CodePen](https://codepen.io/guitimliu/pen/NPGgKwg) | | 03 | 小羊 | [CodePen](https://codepen.io/allen9630308/pen/GgpEKOZ) | | 04 | Clove_墨 | [CodePen](https://codepen.io/CloveTseng1026/pen/yyYXBvj) | | 05 | RUDY | [CodePen](https://codepen.io/Rudy-crw/pen/EaVXYOz) | | 06 | poyi | [CodePen](https://codepen.io/poyi-the-flexboxer/pen/zxvzOgV) | | 07 | dPi | [CodePen](https://codepen.io/snijqlte-the-bold/pen/azvwbdz) | | 08 | Candace | [CodePen](https://codepen.io/Candace802/pen/vENZYeJ) | | 09 | 阿鵝 | [CodePen](https://codepen.io/noracami/pen/bNVRGpM) | | 10 | emmayo | [CodePen](https://codepen.io/emmayo/pen/JoYJooy) | | 11 | DaRon | [CodePen](https://codepen.io/daron0811/pen/gbaRbPZ) | | 12 | yu005620 | [CodePen](https://codepen.io/yu-chin-chiang/pen/EaVXaPm) | | 13 | thchen2002 | [CodePen](https://codepen.io/thchen2002/pen/vENZEJQ) | | 14 | yi | [CodePen](https://codepen.io/5-1/pen/NPGgPLG) | | 15 | Momoze | [CodePen](https://codepen.io/mleczmam-the-typescripter/pen/pvjwJgr) | | 16 | 地瓜 | [CodePen](https://codepen.io/huangtzuchin/pen/LEpLVew) | | 17 | wei_0982 | [CodePen](https://codepen.io/nico-lai/pen/ByoZoam) | | 18 | dean | [CodePen](https://codepen.io/ch933114/pen/xbwrZXp) | | 19 | ann.328 | [CodePen](https://codepen.io/yqmegupa-the-styleful/pen/raOwLzB) | | 20 | 7lun | [CodePen](https://codepen.io/mfyvqhsn-the-bold/pen/MYaoeMw) | | 21 | wanghuaiying | [CodePen](https://codepen.io/huaiying/pen/YPyQGqL) | | 22 | William Hsieh | [CodePen](https://codepen.io/lsaimqxa-the-vuer/pen/LEpLRpO) | | 23 | 阿kai | [CodePen](https://codepen.io/kaihuang3013/pen/GgpENVW) | | 24 | Loder | [CodePen](https://codepen.io/rgbkomhs-the-flexboxer/pen/QwjgdxY) | | 25 | Lin | [CodePen](https://codepen.io/Lin4611/pen/pvjwPbr) | | 26 | kat | [CodePen](https://codepen.io/Nek0u0/pen/PwPjEjL) | | 27 | ya_meow | [Codepen](https://codepen.io/gkfxzvcb-the-bashful/pen/PorjjJW)| | 28 | 地呱 | [Codepen](https://codepen.io/LHung/pen/azvwQWb)| | 29 | haohaoliao | [CodePen](https://codepen.io/yuyuyuhaohao/pen/KwdvpJZ) | | 30 | Sam | [CodePen](https://codepen.io/CloveTseng1026/pen/yyYXBvj) | | 31 | Hugh | [CodePen](https://codepen.io/Hugh-Chen/pen/vENJpMV) | | 32 | 牙山 | [CodePen](https://codepen.io/Tzuru-Chen/pen/ByodYzy) | | 33 | Rexlin | [CodePen](https://codepen.io/Rexlin595/pen/WbQEPdG) | | 34 | David0799 | [CodePen](https://codepen.io/David0799/pen/LEpWbKx) | | 35 | Allen_Lin | [CodePen](https://codepen.io/lcf7891/pen/KwdvYKE) | | 36 | 阿昌 | [CodePen](https://codepen.io/ychleo102615/pen/EaVvJYj) | | 37 | Sam.S.T.Y | [CodePen](https://codepen.io/Sam-Yang-the-animator/pen/gbaxJmV) | | 38 | 黛西 | [CodePen](https://codepen.io/ChiashengLin/pen/myeMYgJ) | | 39 | rio_wei| [CodePen](https://codepen.io/wei_wu/pen/JoYrryv)| | 40 | bonnieli1414| [CodePen](https://codepen.io/bonnieli1414/pen/XJmzNZg)| | 41 | 禹成林 | [CodePen](https://codepen.io/useirin/pen/ZYbaPPM) | | 42 | 肉桂卷 | [CodePen](https://codepen.io/ginnlee/pen/JoYMMEP) | | 43 | Clarence | [CodePen](https://codepen.io/Clarence-Lin/pen/MYaQYpB) | |44|邵|[CodePen](https://codepen.io/ukscrlno-the-typescripter/pen/jEbZPLo)| |45|叮咚|[CodePen](https://codepen.io/pinchieh-lin/pen/WbQzqVr)| |46|白雪|[CodePen](https://codepen.io/weiwei032835-the-styleful/pen/Qwjrgbo)| |47|Chen|[CodePen](https://codepen.io/JGM-C/pen/yyYEXQN)| | 48 | ScarT | [CodePen](https://codepen.io/Acadia/pen/GgpBZvo) | | 49 | Toung | [Codepen](https://codepen.io/Toung/pen/PwPdevv) | | 50 | Rogan | [Codepen](https://codepen.io/RoganHsu/pen/wBMwWNm) | <!-- | 00 | user | [CodePen]() | -->

    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