码迷,mamicode.com
首页 > Web开发 > 详细

[Mise] Update a count state value with the x-on event listener directive in Alpine JS

时间:2020-05-14 19:49:13      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:style   most   --   lis   display   te pro   directive   val   str   

In this lesson, we define a "count" state value with the x-data directive provided by Alpine JS. This defines a new scoped component, which can have access to this state.

We then display the "count" value in our HTML with the x-text directive, which updates an element‘s innerText value.

We also hook up buttons with click event listeners with x-on:click, which allows us to modify the count value as desired.

 

<!-- Define the state for its children -->
<div x-data="{count: 0}">
  <button @click="count--">-1</button>
  <button @click="count++">+1</button>
  <!-- Show the state prop by using x-text -->
  <p>The current count is <strong x-text="count"></strong>.</p>
  <button @click="count = 0">reset</button>
</div>

 

You are not able to access the state outside the outmost div block

[Mise] Update a count state value with the x-on event listener directive in Alpine JS

标签:style   most   --   lis   display   te pro   directive   val   str   

原文地址:https://www.cnblogs.com/Answer1215/p/12890619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!