列表渲染v-for指令写在循环项上:v-for=“(一个参数或者两个参数) in/of 要遍历的数组、对象、字符串、指定次数”
<body>
<div id="app">
<h1>{{msg}}h1>
<h2>遍历数组h2>
<ul>
<li>张三li>
<li>李四li>
<li>王五li>
ul>
<ul>
<li v-for="name of names">{{name}}li>
ul>
<ul>
<li v-for="(name,index) of names"> {{name}}-{{index}}li>
ul>
<h2>遍历对象的属性h2>
<ul>
<li v-for="(value, propertyName) of user">{{propertyName}},{{value}}li>
ul>
<h2>遍历字符串h2>
<ul>
<li v-for="(c,index) of str">{{index}},{{c}}li>
ul>
<h2>遍历指定的次数h2>
<ul>
<li v-for="(num,index) of counter">{{index}}, {{num}}li>
ul>
<table>
<tr>
<th>序号th>
<th>会员名th>
<th>年龄th>
<th>选择th>
tr>
<tr v-for="(vip,index) in vips">
<td>{{index+1}}td>
<td>{{vip.name}}td>
<td>{{vip.age}}td>
<td><input type="checkbox">td>
tr>
table>
div>
<script>
const vm = new Vue({
el : '#app',
data : {
msg : '列表渲染',
names : ['jack','lucy','james'],
vips : [
{id:'111',name:'jack',age:20},
{id:'222',name:'lucy',age:30},
{id:'333',name:'james',age:40}
],
user : {
id : '111',
name : '张三',
gender : '男'
},
str : '动力节点',
counter : 10
}
})
script>
Vue框架采用了虚拟Dom机制+diff算法来提高渲染效率, 只有真正改变的dom元素才会重新渲染
新的虚拟dom和旧的虚拟dom比较原则: 根据v-for指令所在的标签的key属性值(key存在于虚拟dom元素中是其的唯一标识)

如果没有指定标签的key属性,会自动拿index作为key属性的值, 这种方式效率低复用性差, 另外操作数组当中的非末尾元素时容易发生错乱

{{msg}}
序号
姓名
邮箱
选择
{{index + 1}}
{{vip.name}}
{{vip.email}}
指定对象的id作为key属性的值 , 因为id是唯一的所以不会出现错乱问题

监视文本框中输入的数据,根据用户输入的关键字对原数组进行过滤,将新数组渲染到页面
let arr = [1,2,3,4,5,6,7,8,9]
// filter不会破坏原数组的结构,会生成一个全新的数组
let newArr = arr.filter((num) => {
//return 过滤规则
return num < 5
})
console.log(newArr)
<body>
<div id="app">
<h1>{{msg}}h1>
<input type="text" placeholder="请输入搜索关键字" v-model="keyword">
<table>
<tr>
<th>序号th>
<th>英雄th>
<th>能量值th>
<th>选择th>
tr>
<tr v-for="(hero,index) in filteredHeros" :key="hero.id">
<td>{{index+1}}td>
<td>{{hero.name}}td>
<td>{{hero.power}}td>
<td><input type="checkbox">td>
tr>
table>
div>
<script>
const vm = new Vue({
el : '#app',
data : {
keyword : '',
msg : '列表过滤',
heros : [
{id:'101',name:'艾格文',power:10000},
{id:'102',name:'麦迪文',power:9000},
{id:'103',name:'古尔丹',power:8000},
{id:'104',name:'萨尔',power:6000}
],
filteredHeros : []
},
watch : {
// 页面初次加载时就调用handler函数
keyword : {
immediate : true,
handler(val){
this.filteredHeros = this.heros.filter((hero) => {
// 执行过滤规则
return hero.name.indexOf(val) >= 0
})
}
}
}
computed : {
filteredHeros(){
// 返回数组作为计算属性的值
return this.heros.filter((hero) => {
// 执行过滤规则
return hero.name.indexOf(this.keyword) >= 0
})
}
}
})
script>
body>
let arr = [8,9,5,4,1,2,3]
// sort方法排序之后,不会生成一个新的数组,是在原数组的基础之上进行排序,会影响原数组的结构
arr.sort((a, b) => {
return b - a
})
console.log(arr)
<body>
<div id="app">
<h1>{{msg}}h1>
<input type="text" placeholder="请输入搜索关键字" v-model="keyword">
<button @click="type = 1">升序button>
<button @click="type = 2">降序button>
<button @click="type = 0">原序button>
<table>
<tr>
<th>序号th>
<th>英雄th>
<th>能量值th>
<th>选择th>
tr>
<tr v-for="(hero,index) in filteredHeros" :key="hero.id">
<td>{{index+1}}td>
<td>{{hero.name}}td>
<td>{{hero.power}}td>
<td><input type="checkbox">td>
tr>
table>
div>
<script>
const vm = new Vue({
el : '#app',
data : {
type : 0,
keyword : '',
msg : '列表排序',
heros : [
{id:'101',name:'艾格文',power:10000},
{id:'102',name:'麦迪文',power:9000},
{id:'103',name:'古尔丹',power:8000},
{id:'104',name:'萨尔',power:11000}
]
},
computed : {
filteredHeros(){
// 排序会影响原数组的结构
const arr = this.heros.filter((hero) => {
// 执行过滤规则
return hero.name.indexOf(this.keyword) >= 0
})
// 排序
if(this.type === 1){
// a和b是一个对象
arr.sort((a, b) => {
return a.power - b.power
})
}else if(this.type == 2){
arr.sort((a, b) => {
return b.power - a.power
})
}
// 返回新数组作为计算属性的值
return arr
}
}
})
script>
body>
阻止表单的默认提交行为的方式
<body>
<div id="app">
<h1>{{msg}}h1>
<form @submit.prevent="send">
用户名:<input type="text" v-model.trim="user.username"><br><br>
密码:<input type="password" v-model="user.password"><br><br>
年龄:<input type="number" v-model.number="user.age"><br><br>
性别:
男<input type="radio" name="gender" value="1" v-model="user.gender">
女<input type="radio" name="gender" value="0" v-model="user.gender"><br><br>
爱好:
旅游<input type="checkbox" v-model="user.interest" value="travel">
运动<input type="checkbox" v-model="user.interest" value="sport">
唱歌<input type="checkbox" v-model="user.interest" value="sing"><br><br>
学历:
<select v-model="user.grade">
<option value="">请选择学历option>
<option value="zk">专科option>
<option value="bk">本科option>
<option value="ss">硕士option>
select><br><br>
简介:
<textarea cols="50" rows="15" v-model.lazy="user.introduce">textarea><br><br>
<input type="checkbox" v-model="user.accept">阅读并接受协议<br><br>
<button>注册button>
form>
div>
<script>
const vm = new Vue({
el : '#app',
data : {
user : {
username : '',
password : '',
age : '',
// 默认选中指定表单的value值
gender : '1',
// 复选框的value采用数组接收
interest : ['travel'],
grade : 'ss',
introduce : '',
accept : ''
},
msg : '表单数据的收集'
},
methods : {
send(){
alert('ajax...!!!!')
// 将数据收集好,发送给服务器
console.log(JSON.stringify(this.user))
}
}
})
script>
body>
过滤器适用于简单的逻辑处理,可以进行全局配置或局部配置
过滤器可以用在插值语法和v-bind指令中,可以对一些数据进行格式化显示
<body>
<div id="app">
<h1>{{msg}}h1>
<h2>商品价格:{{formatPrice}}h2>
<h2>商品价格:{{formatPrice2()}}h2>
<h2>商品价格:{{price | filterA | filterB(3)}}h2>
<input type="text" :value="price | filterA | filterB(3)">
div>
<div id="app2">
<h2>商品价格:{{price | filterA | filterB(3)}}h2>
div>
<script>
// 配置全局的过滤器可以在另一个容器中使用
Vue.filter('filterA', function(val){
if(val === null || val === undefined || val === ''){
return '-'
}
return val
})
Vue.filter('filterB', function(val, number){
return val.toFixed(number)
})
const vm2 = new Vue({
el : '#app2',
data : {
price : 20.3
}
})
const vm = new Vue({
el : '#app',
data : {
msg : '过滤器',
price : 50.6
},
methods: {
formatPrice2(){
if(this.price === '' || this.price === undefined || this.price === null){
return '-'
}
return this.price
}
},
computed : {
formatPrice(){
if(this.price === '' || this.price === undefined || this.price === null){
return '-'
}
return this.price
}
},
// 局部过滤器
filters : {
filterA(val){
if(val === null || val === undefined || val === ''){
return '-'
}
return val
},
filterB(val, number){
// 确保传递过来的数据val,保留number位小数
return val.toFixed(number)
}
}
})
script>
body>