Vue is a JavaScript framework. It can be added to an HTML page with a <script> tag.
Vue extends HTML attributes with Directives, and binds data to HTML with Expressions.
Vue is a front-end JavaScript framework written in JavaScript.
Similar frameworks to Vue are React and Angular, but Vue is more lightweight and easier to start with.
Vue is distributed as a JavaScript file, and can be added to a web page with a script tag:
<script
src="https://unpkg.com/vue@3/dist/vue.global.js">
</script>
If some of these points are hard to understand, don't worry, you will understand at the end of the tutorial.
There are two different ways to write code in Vue: The Options API and The Composition API.
The underlying concepts are the same for both the Options API and Composition API, so after learning one, you can easily switch to the other.
The Options API is what is written in this tutorial because it is considered to be more beginner-friendly, with a more recognizable structure.
Take a look at this page at the end of this tutorial to learn more about the differences between the Options API and the Composition API.
We will now learn how we can create our very first Vue web page, in 5 basic steps:
<div>
tag with id="app"
for Vue to connect with.<script>
tag with a link to Vue.<script>
tag with the Vue instance inside.<div id="app">
tag.These steps are described in detail below, with the full code in a 'Try It Yourself' example in the end.
Start with a simple HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My first Vue page</title>
</head>
<body>
</body>
</html>
Vue needs an HTML element on your page to connect to.
Put a <div>
tag inside the <body>
tag and give it an id:
<body>
<div id="app"></div>
</body>
To help our browser to interpret our Vue code, add this <script>
tag:
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
Now we need to add our Vue code.
This is called the Vue instance and can contain data and methods and other things, but now it just contains a message.
On the last line in this <script>
tag our Vue instance is connected to the <div id="app">
tag:
<div id="app"></div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
message: "Hello World!"
}
}
})
app.mount('#app')
</script>
Finally, we can use text interpolation, a Vue syntax with double curly braces {{ }}
as a placeholder for data.
<div id="app"> {{ message }} </div>
The browser will exchange {{ message }}
with the text stored in the 'message' property inside the Vue instance.
Here is our very first Vue page:
Test this code with the 'Try it Yourself' button below.
<!DOCTYPE html>
<html lang="en">
<head>
<title>My first Vue page</title>
</head>
<body>
<div id="app">
{{ message }}
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
message: "Hello World!"
}
}
})
app.mount('#app')
</script>
</body>
</html>
Try it Yourself »
Text interpolation is when text is taken from the Vue instance to show on the web page.
The browser receives the page with this code inside:
<div id="app"> {{ message }} </div>
Then the browser finds the text inside the 'message' property of the Vue instance and translates the Vue code into this:
<div id="app">Hello World!</div>
Simple JavaScript expressions can also be written inside the double curly braces {{ }}
.
Use JavaScript syntax to add a random number to the message inside the div element:
<div id="app">
{{ message }} <br>
{{'Random number: ' + Math.ceil(Math.random()*6) }}
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
message: "Hello World!"
}
}
})
app.mount('#app')
</script>
Try it Yourself »
This tutorial will teach you the basics of Vue.
You need basic prior experience with HTML, CSS and JavaScript to follow this tutorial.
Click the 'Next' button to continue with this tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!