Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Basic Syntax

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:**

:

  • This is a
    element with the id hello-vue and class demo.
  • In the Vue application, this
    will be managed by the Vue instance and will update its content when data changes.

{{ message }}:

  • This is Vue.js template syntax, used to bind the message data to the page.
  • When the message data changes, the content on the page updates accordingly.

**JavaScript section explanation:**

Vue instance definition:

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!!'.

Create and mount the Vue application:

  • 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

  • When the page loads, the browser parses HTML and JavaScript.
  • When the Vue.js script executes, it creates a Vue application instance and binds it to
    element in the page.
  • Based on data() 's initial data, it renders message 's value onto the page's {{ message }} .
  • When message When data changes (for example, through user interaction or asynchronous operations), the page automatically updates to reflect those changes.

2. Template Syntax

Interpolation

Vue.js uses double curly braces {{ }} to denote text interpolation:

Example

{{ message }}

Instruction

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.

Link

Shorthand:

Link

v-if / v-else-if / v-else: conditional rendering.



内容可见

内容不可见

v-for: list rendering.

  • {{ item.text }}

v-model: two-way data binding.



Message is: {{ message }}

v-on: event listener.





Shorthand:





3. Event Handling

In Vue.js, you can use the v-on directive to listen for DOM events and execute some JavaScript code when triggered.

Example

 

4. Computed Properties

Computed properties are properties cached based on their dependencies. They are only recomputed when their related dependencies change.

Example

 

{{ reversedMessage }}

5. Components

Components are one of the most powerful features of Vue.js. Components let you build large applications from small, independent, and usually reusable pieces.

Example

const app = createApp({});

app.component('my-component', {

  template: '

A custom component!

'

});

Using Components

Example

 

6. Props and Events

Props

Props are used to pass data between components.

Example

 

Events

Child components trigger events via $emit, and parent components can listen for these events.

Example

 

 

Total clicks: {{ total }}