Vue 3 Basic Syntax
Vue.js is a progressive JavaScript framework mainly used for building user interfaces.
Vue.js is based on the concepts of componentization and reactive data, providing a simple and efficient way to build user interfaces.
Vue Single File Component
The Vue Single File Component (SFC) is a file format of the Vue.js framework that lets developers put HTML, JavaScript, and CSS code in one file, usually with the .vue extension.
A Single File Component is a reusable code organization form that encapsulates the HTML, CSS, and JavaScript belonging to the same component in a file with the .vue extension.
The following are some common Vue.js basic syntax and concepts, including template syntax, directives, event handling, computed properties, and components, which will be explained in detail in later chapters.
1. Create a Vue Instance
Creating a Vue instance is the first step to start using Vue.js; usually you create a Vue instance in an HTML file and mount it to a DOM element.
Example
{{ message }}
Try it »
**HTML section explanation:**
**JavaScript section explanation:** Vue instance definition: Create and mount the Vue application: Vue.js uses double curly braces {{ }} to denote text interpolation: {{ message }} Directives are special attributes prefixed with v- used to express logic in templates. v-bind: dynamically binds one or more attributes, or a component prop. Shorthand: v-if / v-else-if / v-else: conditional rendering. 内容可见 内容不可见 v-for: list rendering. v-model: two-way data binding. Message is: {{ message }} v-on: event listener. Shorthand: In Vue.js, you can use the v-on directive to listen for DOM events and execute some JavaScript code when triggered. Greet Computed properties are properties cached based on their dependencies. They are only recomputed when their related dependencies change. {{ reversedMessage }} Components are one of the most powerful features of Vue.js. Components let you build large applications from small, independent, and usually reusable pieces. const app = createApp({}); app.component('my-component', { template: ' A custom component! ' }); Props are used to pass data between components. Child components trigger events via $emit, and parent components can listen for these events. Total clicks: {{ total }}
hello-vue and class demo.
{{ message }}:
message data to the page.message data changes, the content on the page updates accordingly.const HelloVueApp = {
data() {
return {
message: 'Hello Vue!!'
}
}
}
HelloVueApp is a plain JavaScript object containing Vue component options.data() The method returns an object containing message property, whose initial value is 'Hello Vue!!'.
Vue.createApp() The method is used to create a Vue application instance; the parameter is an object containing component options (here it is HelloVueApp)..mount('#hello-vue') method mounts the Vue application instance to the page element with id="hello-vue" in the page.Execution Process
data() 's initial data, it renders message 's value onto the page's {{ message }} .message When data changes (for example, through user interaction or asynchronous operations), the page automatically updates to reflect those changes.2. Template Syntax
Interpolation
Example
Instruction
Link
Link
3. Event Handling
Example
4. Computed Properties
Example
5. Components
Example
Using Components
Example
6. Props and Events
Props
Example
Events
Example