官术网_书友最值得收藏!

Displaying a list with v-for

We will now display the list of notes below the toolbar.

  1. Just below the toolbar, add a new p element with the ;notes class:
      <aside class="side-bar">
        <p class="toolbar">
          <button @click="addNote"><i class="material-icons">add</i>        
Add note</button> </p> <p class="notes"> <!-- Note list here --> </p> </aside>

Now, we want to display a list of multiple p elements, one for each note. To achieve this, we need the v-for directive. It takes a special expression as the value, in the form of item of items, that will iterate over the items array or object and expose an item value for this part of the template. Here is an example:

<p v-for="item of items">{{ item.title }}</p>

You can also use the in keyword instead of of:

<p v-for="item in items">{{ item.title }}</p>

Imagine that we have the following array:

data () {
  return {
    items: [
      { title: 'Item 1' },
      { title: 'Item 2' },
      { title: 'Item 3' },
    ]
  }
}

The final rendered DOM will look like this:

<p>Item 1</p>
<p>Item 2</p>
<p>Item 3</p>

As you can see, the element on which you put the v-for directive is repeated in the DOM.

  1. Let's go back to our notebook and display the notes in the side pane. We store them in the notes data property, so we need to iterate over it:
      <p class="notes">
        <p class="note" v-for="note of notes">{{note.title}}</p>
      </p>

We should now have the notes list displayed below the button:

Add a few more notes using the button, and you should see that the list is updating automatically!

主站蜘蛛池模板: 汤阴县| 东丽区| 宜兰市| 临泉县| 海口市| 宁乡县| 湖北省| 神木县| 玛沁县| 酉阳| 涿州市| 正镶白旗| 金坛市| 晋城| 青铜峡市| 遂溪县| 长宁区| 瑞丽市| 曲麻莱县| 开封市| 左云县| 炎陵县| 吉木萨尔县| 乐清市| 巴青县| 准格尔旗| 都匀市| 陈巴尔虎旗| 封开县| 和田市| 海淀区| 乐清市| 通州市| 民乐县| 沁源县| 镶黄旗| 文化| 珲春市| 北流市| 舟曲县| 芦山县|