1. 插值语法
1.1 基本使用
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h2>counter: {{counter}}</h2>
</div>
<script src="./lib/vue.js"></script>
<script>
const app = Vue.createApp({
data: function(){
return {
counter: 100
}
}
})
app.mount("#app")
</script>
</body>
</html>
1.2 表达式
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h2>counter: {{counter}}</h2>
</div>
<script src="./lib/vue.js"></script>
<script>
const app = Vue.createApp({
data: function(){
return {
counter: 100
}
}
})
app.mount("#app")
</script>
</body>
</html>
1.3 三元表达式
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h2>age: {{ age> 18 ? '成年人' : '未成年' }}</h2>
</div>
<script src="./lib/vue.js"></script>
<script>
const app = Vue.createApp({
data: function(){
return {
age: 19
}
}
})
app.mount("#app")
</script>
</body>
</html>
2. 常用指令
2.1 v-once指令
v-once用于指定元素或者组件只渲染一次
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 这个元素渲染一次后,下面虽然定义了方法来改变值,但是v-once不会让这个元素改变了 -->
<h2 v-once>
{{ message }}
</h2>
<h1>{{ message }}</h1>
<button @click="handle">改变message</button>
</div>
<script src="./lib/vue.js"></script>
<script>
const app = Vue.createApp({
data: function(){
return {
message: "hello world"
}
},
methods: {
handle: function(){
this.message = "你好,世界"
}
}
})
app.mount("#app")
</script>
</body>
</html>