tips:
('abc')indexOf('c') == 3
('abc')indexOf('') == 0
//indexof认为所有字符串里都有空串
<body>
<div id="watch">
<h1>single:divah1>
<input type="text" v-model='keyword'>
<ul>
<li v-for="(p,index) of filpersons" :key='p.id'>
{{p.name}}-{{p.age}}
li>
ul>
div>
body>
<script type="text/javascript">
new Vue({
el: "#watch",
data: {
keyword: "",
persons: [
{ id: "001", name: "gaga", age: '34' },
{ id: "002", name: "adele", age: '20' },
{ id: "003", name: "beyonce", age: '30' },
{ id: "004", name: "cardib", age: '40' }
],
filpersons: []
},
//watch写法
watch: {
keyword: {
immediate: true,
handler(newk) {
this.filpersons = this.persons.filter((p) => {
return p.name.indexOf(newk) !== -1;
}
)
}
}
},
})
</script>
<script type="text/javascript">
new Vue({
el: "#watch",
data: {
keyword: "",
persons: [
{ id: "001", name: "gaga", age: '34' },
{ id: "002", name: "adele", age: '20' },
{ id: "003", name: "beyonce", age: '30' },
{ id: "004", name: "cardib", age: '40' }
],
},
//compute写法
computed: {
filpersons() {
return this.persons.filter((p) => { //这里p是this.persons的形参
return p.name.indexOf(this.keyword) !== -1;
}
)
}
}
})
</script>
<button @click = "sortType=0">原序button>
<button @click = "sortType=1">降序button>
<button @click = "sortType=2">升序button>
computed: {
filpersons() {
const arr = this.persons.filter((p) => { //这里p是this.persons的形参
return p.name.indexOf(this.keyword) !== -1;
}
)
if(this.sortType){
arr.sort((p1,p2)=>{
return this.sortType === 1? p2.age-p1.age : p1.age - p2.age
})
}
return arr
}
}
问题引入:
当年忘写了sex这个项,后来想要添加
使用api:Vue.set()
>官方文档
或 vm.$set()
(_data
可以去掉)
问题引入
当我想要改变第一个人的信息时,第一种方法可以,第二种方法失效
原因:vue认为,只有在对数组进行了pop\push\shift\unshift\splice\sort\reverse
7种基本操作才会对页面进行更新,(VUE对原生的这七中方法重写了)所以上面这个直接修改数组元素的方法失效
可以这样改:
汇总了上面VUE监测的知识点:
YOUTUBE地址
P.S. 这一节在37课,这一节的练习也应该跟着敲一下的,但是我没有时间就先跳过去了,有机会一定要回来敲一下。
Vue监视数据的原理:
Vue.set(target,propertyName/index,value)
或vm.$set(target,propertyName/index,value)
push()、pop()、shift()、unshift()、splice()、sort()、reverse()
Vue.set()
或 vm.$set()
Vue.set()
和 vm.$set()
不能给vm 或 vm的根数据对象 添加属性!!!收集表单数据:
,则v-model收集的是value值,用户输入的就是value值。
,则v-model收集的是value值,且要给标签配置value值。
容器代码:
<div id="root">
<form @submit.prevent="demo">
账号:<input type="text" v-model.trim="userInfo.account"> <br/><br/>
密码:<input type="password" v-model="userInfo.password"> <br/><br/>
年龄:<input type="number" v-model.number="userInfo.age"> <br/><br/>
性别:
男<input type="radio" name="sex" v-model="userInfo.sex" value="male">
女<input type="radio" name="sex" v-model="userInfo.sex" value="female"> <br/><br/>
爱好:
学习<input type="checkbox" v-model="userInfo.hobby" value="study">
打游戏<input type="checkbox" v-model="userInfo.hobby" value="game">
吃饭<input type="checkbox" v-model="userInfo.hobby" value="eat">
<br/><br/>
所属校区
<select v-model="userInfo.city">
<option value="">请选择校区option>
<option value="beijing">北京option>
<option value="shanghai">上海option>
<option value="shenzhen">深圳option>
<option value="wuhan">武汉option>
select>
<br/><br/>
其他信息:
<textarea v-model.lazy="userInfo.other">textarea> <br/><br/>
<input type="checkbox" v-model="userInfo.agree">阅读并接受<a href="http://www.atguigu.com">《用户协议》a>
<button>提交button>
form>
div>
js代码:
new Vue({
el:'#root',
data:{
userInfo:{
account:'',
password:'',
age:18,
sex:'female',
hobby:[],
city:'beijing',
other:'',
agree:''
}
},
methods: {
demo(){
console.log(JSON.stringify(this.userInfo))
}
}
})
过滤器 不看了 VUE3删除了
内置指令
v-cloak指令(没有值):
需求1:定义一个v-big指令,和v-text功能类似,但会把绑定的数值放大10倍。
在这之后的代码都放在github上面了,这里不再更新