{{ }} 显示值
用于在template属性中显示值其他属性定义的变量的值、函数返回的值、表达式的值
1、基本
数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值:
<!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"></div>
<template id="my-app">
<!-- 这里会显示为:Hello World -->
<h2>{{message}}</h2>
<!-- 相当于<h2>Hello World</h2> -->
</template>
<script src="https://unpkg.com/vue@next"></script>
<script>
const App = {
template: '#my-app',
data() {
return {
message: "Hello World",
counter: 100,
isShow: true
}
},
methods: {
getReverseMessage() {
return this.message.split(" ").reverse().join(" ");
},
toggle() {
this.isShow = !this.isShow;
}
}
}
Vue.createApp(App).mount('#app');
</script>
</body>
</html>
Mustache 标签将会被替代为对应数据对象上 message变量 的值。无论何时,绑定的数据对象上 message变量 发生了改变,插值处的内容都会更新。
2、计算表达式
<template id="my-app">
<h2>{{counter * 10}}</h2>
<h2>{{ message.split(" ").reverse().join(" ") }}</h2>
</template>
如果计算方式比较复杂,就不建议直接放这里,应该用计算属性
https://www.yuque.com/yejielin/mypn47/bilegi#EF7y8
3、显示函数返回值
<template id="my-app">
<h2>{{getReverseMessage()}}</h2>
</template>
...
<script src="../js/vue.js"></script>
<script>
const App = {
template: '#my-app',
data() {
return {
message: "Hello World",
counter: 100,
isShow: true
}
},
methods: {
getReverseMessage() {
return this.message.split(" ").reverse().join(" ");
},
toggle() {
this.isShow = !this.isShow;
}
}
}
Vue.createApp(App).mount('#app');
</script>
4、显示三元运算符的值
<template id="my-app">
<!-- 4.三元运算符 -->
<h2>{{ isShow ? "哈哈哈": "" }}</h2>
</template>
其他
语句、流程控制等不会生效