EN
Vue.js - use Vue in HTML web page (.html file)
0 points
In this article, we would like to show you how to use Vue.js in HTML web page.
1. In your .html
file, you need to attach the following <script>
tag to use Vue.js via CDN:
xxxxxxxxxx
1
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
Note:
In References section below, you can find link to the official documentation with the latest version of CDN.
2. Create HTML element with id="app"
that will be a representation of Vue.js application:
xxxxxxxxxx
1
<div id="app">Example text...</div>
3. Create a <script>
tag inside web page body and import createApp
from Vue
.
xxxxxxxxxx
1
<script>
2
const { createApp } = Vue;
3
</script>
4. Inside the <script>
, use createApp
to create Vue application and mount it to the #app
element.
xxxxxxxxxx
1
<script>
2
const { createApp } = Vue;
3
4
createApp({
5
data() {
6
return {
7
message: 'Hello from Vue!'
8
}
9
}
10
}).mount('#app');
11
12
</script>
In this example, we present how to bind Vue.js to the HTML element and display some message
stored as the Vue object data
.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
5
</head>
6
<body>
7
<div id="app">{{ message }}</div>
8
<script>
9
const { createApp } = Vue;
10
11
createApp({
12
data() {
13
return {
14
message: 'Hello from Vue!'
15
}
16
}
17
}).mount('#app');
18
19
</script>
20
</body>
21
</html>