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

Button and click events with v-on

Now, we will need a button to call this method. Create a new button element inside a p element with the toolbar class:

<aside class="side-bar">
  <!-- Toolbar -->
  <p class="toolbar">
    <!-- Add note button -->
    <button><i class="material-icons">add</i> Add note</button>
  </p>
</aside>

To call the addNote method when the user clicks on the button, we will need a new directive--v-on. The value will be the function called when the event is caught, but it also expects an argument to know which event to listen to. However, how do we pass the argument to the directive, you might ask? It's quite simple! Add a : character after the directive name, followed by the argument. Here is an example:

<button v-directive:argument="value">

In our case, we are using the v-on directive with the event name as the argument, and more specifically, the click event. It should look like this:

<button v-on:click="callback">

Our button should call the addNote method when it is clicked on, so go ahead and modify the button we added earlier:

<button v-on:click="addNote"><i class="material-icons">add</i> Add note</button>

There is an optional special shortcut for the v-on directive--the @ character that allows you to rewrite the preceding ;code to the following:

<button @click="addNote"><i class="material-icons">add</i> Add note</button>

Now that our button is ready, try adding a few notes. We don't see them in the app yet, but you can open the devtools and note the note list change:

主站蜘蛛池模板: 肇源县| 凌源市| 忻州市| 凌源市| 井冈山市| 罗平县| 平泉县| 中山市| 濮阳市| 汤原县| 东港市| 临猗县| 肇庆市| 广汉市| 怀远县| 平武县| 维西| 葫芦岛市| 雷山县| 裕民县| 云霄县| 永德县| 贵德县| 佛教| 海林市| 北票市| 女性| 乌海市| 阳城县| 广南县| 彝良县| 鲜城| 台北县| 林周县| 开远市| 阳朔县| 娄底市| 丰城市| 柳林县| 普兰县| 西平县|