DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>基本列表title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>人员列表(遍历数组)h2>
<ul>
<li v-for="(p,index) of persons" :key="index">
{{p.name}}-{{p.age}}
li>
ul>
<h2>显卡信息(遍历对象)h2>
<ul>
<li v-for="(value,k) of xianka" :key="k">
{{k}}-{{value}}
li>
ul>
<h2>测试遍历字符串(用得少)h2>
<ul>
<li v-for="(char,index) of str" :key="index">
{{char}}-{{index}}
li>
ul>
<h2>测试遍历指定次数(用得少)h2>
<ul>
<li v-for="(number,index) of 5" :key="index">
{{index}}-{{number}}
li>
ul>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
persons:[
{ id: '001', name: 'Mr.Wang', age: 18 },
{ id: '002', name: 'Miss.Wang', age: 19 },
{ id: '003', name: 'SomeOneElse', age: 20 }
],
xianka:{
name: 'RTX3090',
price: '1.5w',
level: '999+'
},
str: 'hello'
}
})
script>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>key的原理title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>人员列表(遍历数组)h2>
<button @click.once="add">添加一个张先生button>
<ul>
<li v-for="(p,index) of persons" :key="index">
{{p.name}}-{{p.age}}
<input type="text">
li>
ul>
div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
persons:[
{id: '001', name: 'Mr.Wang', age: 18},
{id: '002', name: 'Miss.Wang', age: 19},
{id: '003', name: 'SomeOneElse', age: 20}
]
},
methods: {
add(){
const p = {id: '004', name: 'Mr.Zhang', age: 40}
this.persons.unshift(p)
}
},
})
script>
html>
说明:
其实也不能说事诡异一幕,内部原理就是Vue虚拟DOM的复用规则导致的
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>列表过滤title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>人员列表h2>
<input type="text" placeholder="请输入名字" v-model="keyWord">
<ul>
<li v-for="(p,index) of filPerons" :key="index">
{{p.name}}-{{p.age}}-{{p.sex}}
li>
ul>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
keyWord: '',
persons: [
{id: '001', name: 'Miss.Wang', age: 19, sex:'女'},
{id: '002', name: 'Miss.Li', age: 20, sex:'女'},
{id: '003', name: 'Mr.Wang', age: 18, sex:'男'},
{id: '004', name: 'Mr.Li', age: 21, sex:'男'}
]
},
computed:{
filPerons(){
return this.persons.filter((p)=>{
return p.name.indexOf(this.keyWord) !== -1
})
}
}
})
script>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>列表排序title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>人员列表h2>
<input type="text" placeholder="请输入名字" v-model="keyWord">
<button @click="sortType = 2">年龄升序button>
<button @click="sortType = 1">年龄降序button>
<button @click="sortType = 0">原顺序button>
<ul>
<li v-for="(p,index) of filPerons" :key="p.id">
{{p.name}}-{{p.age}}-{{p.sex}}
li>
ul>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
keyWord:'',
sortType:0, //0原顺序 1降序 2升序
persons:[
{id: '001', name: 'Miss.Wang', age: 19, sex:'女'},
{id: '002', name: 'Miss.Li', age: 20, sex:'女'},
{id: '003', name: 'Mr.Wang', age: 18, sex:'男'},
{id: '004', name: 'Mr.Li', age: 21, sex:'男'}
]
},
computed:{
filPerons(){
const arr = this.persons.filter((p)=>{
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
}
}
})
script>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>更新时的一个问题title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h2>人员列表h2>
<button @click="updateFir_each">更新第一人的信息Fun1button><br/><br/>
<button @click="updateFir_Obj">更新第一人的信息Fun2button><br/><br/>
<button @click="updateFir_splice">更新第一人的信息Fun3button>
<ul>
<li v-for="(p,index) of persons" :key="p.id">
{{p.name}}-{{p.age}}-{{p.sex}}
li>
ul>
div>
<script type="text/javascript">
Vue.config.productionTip = false
const vm = new Vue({
el:'#root',
data:{
persons:[
{id: '001', name: 'Miss.Wang', age: 19, sex:'女'},
{id: '002', name: 'Miss.Li', age: 20, sex:'女'},
{id: '003', name: 'Mr.Wang', age: 18, sex:'男'},
{id: '004', name: 'Mr.Li', age: 21, sex:'男'}
]
},
methods: {
updateFir_each(){ //奏效
this.persons[0].name = '小明'
this.persons[0].age = 12
this.persons[0].sex = '男'
},
updateFir_Obj(){ //不奏效
this.persons[0] = {id: '001', name: '小明', age: 12, sex: '男'}
},
updateFir_splice(){
this.persons.
splice(0,
1,
{id: '001', name: '小明', age: 12, sex: '男'})
}
}
})
script>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>错误数据监测title>
head>
<body>
<script type="text/javascript" >
let data = {
name: 'Mr.Wang'
}
Object.defineProperty(data, 'name', {
get(){
return data.name
},
set(val){
data.name = val
}
})
script>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>模拟数据监测title>
head>
<body>
<script type="text/javascript" >
let data = {
name: 'Mr.Wang',
age: 24,
}
//创建一个监视的实例对象,用于监视data中属性的变化
const obj = new Observer(data)
console.log(obj)
//准备一个vm实例对象
let vm = {}
vm._data = data = obj
function Observer(obj){
//汇总对象中所有的属性形成一个数组
const keys = Object.keys(obj)
//遍历
keys.forEach((key)=>{
Object.defineProperty(this, key, {
get(){
return obj[key]
},
set(val){
console.log(`${key}被改了,开始解析模板,生成虚拟DOM...`)
obj[key] = val
}
})
})
}
script>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Vue.set的使用title>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h1>学生信息h1>
<button @click.once="addSex">添加一个性别属性(默认男)button>
<h2>姓名:{{student.name}}h2>
<h2 v-if="student.sex">性别:{{student.sex}}h2>
<h2>年龄:真实{{student.age.rAge}},对外{{student.age.sAge}}h2>
<h2>朋友们h2>
<ul>
<li v-for="(f,index) in student.friends" :key="index">
{{f.name}}--{{f.age}}
li>
ul>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
const vm = new Vue({
el: '#root',
data:{
student: {
name: 'Mr.Wang',
age: {
rAge: 24,
sAge: 18,
},
friends: [
{name: '旺财', age: 6},
{name: '咪咪', age: 3}
]
}
},
methods: {
addSex(){
// Vue.set(this.student,'sex','男')
this.$set(this.student,'sex','男')
}
}
})
script>
html>
Vue是通过包装数组身上的常用修改数组的方法实现的,类似对象的监测,在调用修改方法之后调用模板检测实现动态更新
还可以使用Vue.set/vm.$set
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>总结数据监视title>
<style>
button{
margin-top: 10px;
}
style>
<script type="text/javascript" src="../js/vue.js">script>
head>
<body>
<div id="root">
<h1>学生信息h1>
<button @click="student.age++">年龄+1岁button> <br/>
<button @click="addSex">添加性别属性,默认值:男button> <br/>
<button @click="student.sex = '未知' ">修改性别button> <br/>
<button @click="addFriend">在列表首位添加一个朋友button> <br/>
<button @click="updateFirstFriendName">修改第一个朋友的名字为:张三button> <br/>
<button @click="addHobby">添加一个爱好button> <br/>
<button @click="updateHobby">修改第一个爱好为:开车button> <br/>
<button @click="removeSmoke">过滤掉爱好中的抽烟button> <br/>
<h3>姓名:{{student.name}}h3>
<h3>年龄:{{student.age}}h3>
<h3 v-if="student.sex">性别:{{student.sex}}h3>
<h3>爱好:h3>
<ul>
<li v-for="(h,index) in student.hobby" :key="index">
{{h}}
li>
ul>
<h3>朋友们:h3>
<ul>
<li v-for="(f,index) in student.friends" :key="index">
{{f.name}}--{{f.age}}
li>
ul>
div>
body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
const vm = new Vue({
el: '#root',
data:{
student:{
name: 'Mr.Wang',
age: 18,
hobby:['抽烟', '喝酒', '烫头'],
friends:[
{name: '旺财',age: 8},
{name: '咪咪',age: 3}
]
}
},
methods: {
addSex(){
// Vue.set(this.student,'sex','男')
this.$set(this.student,'sex','男')
},
addFriend(){
this.student.friends.unshift({name:'jack',age:70})
},
updateFirstFriendName(){
this.student.friends[0].name = '张三'
},
addHobby(){
this.student.hobby.push('学习')
},
updateHobby(){
// this.student.hobby.splice(0,1,'开车')
// Vue.set(this.student.hobby,0,'开车')
this.$set(this.student.hobby,0,'开车')
},
removeSmoke(){
this.student.hobby = this.student.hobby.filter((h)=>{
return h !== '抽烟'
})
}
}
})
script>
html>