<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="app"> <p>计数器的值是:{{ counter }}</p> <button v-on:click="add">增加</button> <button v-on:click="minus">减少</button> </div>
<p>
label to display the counter value, here passed { {}}
syntax to bind Vue data, that is, the value of the counter
variable. In the two buttons, we bound the add
and minus
methods respectively, and specified the click event v-on:click
.
<p>Finally, define the Vue instance in JavaScript, and define counter
variables and corresponding methods:
new Vue({ el: '#app', data: { counter: 0 }, methods: { add: function() { this.counter++; }, minus: function() { this.counter--; } } })
attribute specifies the area to be controlled by Vue, that is, the DIV area with
id="app" defined above. The
counter variable is defined in the
data attribute and its initial value is 0.
methodsTwo methods are defined in the attribute, one is used to increase the value of the counter, and the other is used to decrease the value of the counter.
Now, when we open the HTML page, we can see that a counter appears on the page, with an initial value of 0. When we click the "Increase" button, the counter value will increase by 1; when we click the "Decrease" button, the counter value will decrease by 1. This is the first example of Vue. <p>There is still a long way to go in learning Vue, but through this simple counter example, we can have a general understanding of the basic usage of Vue. Next, we can continue to learn Vue’s components, instructions, filters and other advanced usage to make front-end development easier and more efficient. <p>The above is the detailed content of Vue learning practice: create a simple counter. For more information, please follow other related articles on the PHP Chinese website!