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

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:

主站蜘蛛池模板: 安远县| 和龙市| 塘沽区| 含山县| 柳河县| 孟连| 临高县| 宁都县| 岗巴县| 上饶市| 琼中| 白玉县| 栖霞市| 舟曲县| 韶山市| 靖宇县| 白水县| 天长市| 澄城县| 临城县| 寻乌县| 阳谷县| 河池市| 高州市| 灌阳县| 泸定县| 夹江县| 江达县| 延川县| 贺州市| 维西| 于都县| 乌兰察布市| 牡丹江市| 抚松县| 黄龙县| 平阴县| 枣强县| 蓬溪县| 绩溪县| 吴堡县|