v-bind
v-bind
绑定元素属性。
指令前缀带有前缀v-
,表示它们是Vue提供的特殊特性,它们会在渲染的DOM上应用特殊的响应式行为。
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>v-bind</title>
</head>
<body>
<!--view层 模板-->
<div id="app">
<span v-bind:title="message">
鼠标悬停查看
</span>
</div>
<!--导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
var vm=new Vue({
el: "#app",
//Model层 数据
data:{
message: "span message"
}
});
</script>
</body>
</html>
v-if,v-else 条件渲染
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断</title>
</head>
<body>
<!--view层 模板-->
<div id="app">
<h1 v-if="ok">Yes</h1>
<h1 v-else>No</h1>
</div>
<!--导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
var vm=new Vue({
el: "#app",
//Model层 数据
data:{
ok: true
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断</title>
</head>
<body>
<div id="app">
<h1 v-if="type==='A'">A</h1>
<h1 v-else-if="type==='B'">B</h1>
<h1 v-else-if="type==='C'">C</h1>
<h1 v-else>D</h1>
</div>
<!--导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
var vm=new Vue({
el: "#app",
data:{
type: 'B'
}
});
</script>
</body>
</html>
v-for 列表渲染
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循环</title>
</head>
<body>
<div id="app">
<li v-for="item in items">
{{item.message}}
</li>
</div>
<!--导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
var vm=new Vue({
el: "#app",
data:{
items: [
{message: '苹果'},
{message: '香蕉'},
{message: '橙子'},
]
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循环</title>
</head>
<body>
<div id="app">
<li v-for="(item,index) in items">
{{index}} - {{item.message}}
</li>
</div>
<!--导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
var vm=new Vue({
el: "#app",
data:{
items: [
{message: '苹果'},
{message: '香蕉'},
{message: '橙子'},
]
}
});
</script>
</body>
</html>