我们前面的课程其实已经使用过Vue的模板语法了,就是双花括号中使用变量 {{name}},那么这种语法其实是Vue中的插值语法。
Vue中包含两种语法:
1.插值语法
功能:用于解析标签体内容。
写法:{{xxx}},xxx是js表达式,且可以直接读取到data中的所有属性。
2.指令语法
功能:用于解析标签(包括:标签属性、标签体内容、绑定事件...)
举例:v-bind:href = "xxx" 或简写为 :href="xxx",xxx同样要写js表达式,且可以直接读取到data中的所有属性。
备注:Vue中有很多的指令,且形式都是: v-??,下面我们会用v-bind举例。
<!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>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>插值语法</h1>
<h3>你好,{{name}}</h3>
<h1>指令语法</h1>
<a v-bind:href="url" x="hello">点我去{{page.name}}1</a>
<a :href="url" :x="x">点我去{{page.name}}2</a>
</div>
<hr>
</body>
<script>
Vue.config.productionTip = false;
new Vue({
el:'#root',
data:{
name:'张三',
x:"hello",
// vue中的data是可以有层级的
page:{
name:"csdn首页"
}
}
})
</script>
</html>
执行结果: