3.Vue基本语法
v-bind (v-指令)简写::
使用v-bind
属性来绑定属性
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>v-bind练习</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
<div id="ddd">
<!-- span的title属性可以通过鼠标悬停查看title的值,v-bind将值绑定到title -->
<span v-bind:title="message">
鼠标悬停获取绑定信息
</span>
</div>
<script>
//创建vue对象
var vm = new Vue({
el : "#ddd",
data : {
message : "hello vue"
}
});
</script>
</body>
</html>
v-
指令是Vue提供的特殊特性。会在渲染的DOM上应用特殊的响应式行为
上述代码的意思为 : 将这个元素节点的title特性和Vue实例的message属性保持一致
判断-循环
判断
<!--if - else -->
<body>
<div id="app">
<h1 v-if="ok">YES</h1><!-- v-开头的标签可以直接取出来ModelView的值 -->
<h1 v-else>NO</h1>
</div>
<script>
var vm = new Vue({
el : "#app",
data : {
ok : true
}
});
</script>
</body>
<!-- if else-if else -->
<body>
<div id="app">
<h1 v-if="type==='A'">A</h1> <!--如果ModelView type==A-->
<h1 v-else-if="type==='B'">B</h1> <!--如果ModelView type==B-->
<h1 v-else>C</h1> <!--如果ModelView type==C-->
</div>
<script>
var vm = new Vue({
el : "#app",
data : {
type : 'A'
}
});
</script>
</body>
循环
<body>
<div id="app">
<li v-for="item in items"> <!-- var in list v-for遍历数组 -->
{{item}} <!--输出元素-->
</li>
</div>
<script>
var vm = new Vue({
el : "#app",
data : {
items : [
'a',
'b',
'c'
]
}
});
</script>
</body>
<body>
<div id="app">
<li v-for="item in items">
{{item.message}} <!--输出message属性的值-->
</li>
</div>
<script>
var vm = new Vue({
el : "#app",
data : {
items : [
//元素是两个对象
{message:'a'}, //键值对 message 属性的值为 'a'
{message:'b'}
]
}
});
</script>
</body>
<p v-for="(item,index) in items"></p>
<!--同时获取遍历的元素item和遍历的索引index-->