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

Vue 3 Getting Started

Vue 3 Getting Started

Vue.js is a progressive JavaScript framework mainly used for building user interfaces.

When first learning Vue, we do not recommend using the vue-cli command-line tool to create a project; a simpler way is to directly include the vue.global.js file in the page for testing and learning.

A Vue 3 application is created using the createApp function, with the following syntax:

const app = Vue.createApp({ /* 根组件选项 */ })

The options passed to createApp are used to configure the root component.

The application instance is only rendered after the .mount() method is called. The .mount() method takes a "container" argument, which can be an actual DOM element or a CSS selector string:

app.mount('#app')

A simple example:

Vue.createApp(HelloVueApp).mount('#hello-vue')

The argument to createApp is the root component (HelloVueApp); when the app is mounted, this component is the starting point of rendering.

An application needs to be mounted into a DOM element; the code above uses mount('#hello-vue') to mount the Vue application HelloVueApp into

it.

Next, we begin with the code from Hello Vue!! to start learning.

Vue 3.0 Instance

<div id="hello-vue" class="demo">

{{ message }}

div

​

<

script

const HelloVueApp = { data() { return { message: 'Hello Vue!!' } } } ​ Vue.createApp(HelloVueApp).mount('#hello-vue')

script

Try it »

Click the "Try it" button to view the online example

In the example above, we first include Vue's JS file in the HTML page:



There is a div element in the HTML page:

{{ message }}

mount('#hello-vue') mounts the Vue application HelloVueApp to

it.

{{ }} is used to output object properties and function return values.

{{ message }} corresponds to message value.

Root Component Template

The root component's template is usually embedded in the component definition, but it can also be specified separately by defining the template directly inside the mount point's container.

Example

 

Try it »

When the root component does not set a template option, Vue will automatically use the container's innerHTML as the template.

Code explanation:

data Option

data Optionis a function. Vue calls this function during the creation of a new component instance. It should return an object, which Vue then wraps through its reactivity system and stores in the component instance as $data.

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

count

:

4

}

 

}

}

)

const

vm

=

app.

mount

(

'#app'

)

document.

write

(

vm.$data.

count

)

// => 4

document.

write

(

"
"

)

document.

write

(

vm.

count

)

     

// => 4

document.

write

(

"
"

)

// modifying vm.count will also update $data.count

vm.

count

=

5

document.

write

(

vm.$data.

count

)

// => 5

document.

write

(

"
"

)

// and vice versa

vm.$data.

count

=

6

document.

write

(

vm.

count

)

// => 6

Try it »

The instance properties above are only added when the instance is first created, so you need to ensure they are all in the object returned by the data function.

Method

We can add methods to a component using the methods option, which is an object containing the required methods.

In the example below we add the methods option, which contains the increment() method:

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

count

:

4

}

 

}

,

  methods

:

{

    increment

(

)

{

     

// this points to the component instance

     

this

.

count

++

   

}

 

}

}

)

const

vm

=

app.

mount

(

'#app'

)

document.

write

(

vm.

count

)

// => 4

document.

write

(

"
"

)

vm.

increment

(

)

document.

write

(

vm.

count

)

// => 5

Try it »