Vue learning practice: create a simple counter
<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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.
