0
点赞
收藏
分享

微信扫一扫

【Vue】基础系列(十三)vue.set()方法-非原型对象修改数组内容-总结Vue监视数据

凯约 2022-09-27 阅读 51


和阿牛一起冲Vue


文章目录

  • ​​和阿牛一起冲Vue​​
  • ​​vue.set()方法​​
  • ​​修改数组内容的方法​​
  • ​​总结Vue监视数据​​
  • ​​实例code:​​

vue.set()方法

只能给data之下的对象添加属性

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>更新时的一个问题</title>
</head>

<body>
<div id="root">
<h1>{{name}}</h1>
<h2>{{student.name}}</h2>
<button @click="addsex">点击添加性别</button>
<h2 v-if="student.sex">{{student.name}}:{{student.sex}}</h2>

</div>
</body>
<script src='vue.js'></script>
<script>
Vue.config.productionTip = false;
new Vue({
el: '#root',
data: {
name: 'jack',
message: [
{ id: '001', name: '马冬雨', age: 18 },
{ id: '002', name: '周冬梅', age: 19 },
{ id: '003', name: '周杰伦', age: 10 },
{ id: '004', name: '温兆伦', age: 23 }
],
student: {
name: '阿花',
}
},
methods: {
addsex() {
Vue.set(this.student, 'sex', '女')
}
},
});
</script>

</html>

修改数组内容的方法

7个操作数组的数据API

push
shift
splice
……

总结Vue监视数据

Vue监视数据的原理:

  • vue会监视data中所有层次的数据。

2. 如何监测对象中的数据?
通过setter实现监视,且要在new Vue时就传入要监测的数据。

  • (1).对象中后追加的属性,Vue默认不做响应式处理
  • (2).如需给后添加的属性做响应式,请使用如下API:
    Vue.set(target,propertyName/index,value) 或
    vm.$set(target,propertyName/index,value)

3. 如何监测数组中的数据?

通过包裹数组更新元素的方法实现,本质就是做了两件事:

  • (1).调用原生对应的方法对数组进行更新。
  • (2).重新解析模板,进而更新页面。

4.在Vue修改数组中的某个元素一定要用如下方法:

  • 1.使用这些API:push()、pop()、shift()、unshift()、splice()、sort()、reverse()
  • 2.Vue.set() 或 vm.$set()

特别注意:Vue.set() 和 vm.$set() 不能给vm 或 vm的根数据对象 添加属性!!!

【Vue】基础系列(十三)vue.set()方法-非原型对象修改数组内容-总结Vue监视数据_vue

实例code:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</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="updatafriendsname">将第一个朋友的名字改为张三</button><br>
<button @click="addHobby">添加一个爱好</button><br>
<button @click="updataHobby">修改第一个爱好</button><br>
<button @click="removeSmoke">过滤掉抽烟爱好</button><br>






<h4>name:{{student.name}}</h4>
<h4>age:{{student.age}}</h4>
<h4 v-if="student.sex">sex:{{student.sex}}</h4>

<h4>Bobby:</h4>
<ul>
<li v-for="(p,index) in student.hobby" :key="index">
{{p}}
</li>
</ul>
<h4>friends:</h4>
<ul>
<li v-for="(p,index) in student.friends" :key="index">
{{p.name}}-{{p.age}}
</li>
</ul>

</div>
</body>
<script src='vue.js'></script>
<script>
Vue.config.productionTip = false;
new Vue({
el: '#root',
data: {
student: {
name: 'tom',
age: 18,
hobby: ['喝酒', '抽烟', '烫头'],
friends: [
{ name: 'jack', age: 17 },
{ name: "mary", age: 19 }
]
}
},
methods: {
addsex() {
Vue.set(this.student, "sex", '男')
// this.$set(this.student, "sex", '男')
},
addfriend() {
this.student.friends.unshift({ name: 'TONG', age: 20 })
},
updatafriendsname() {
this.student.friends[0].name = '张三'
},
addHobby() {
this.student.hobby.push('学习');
},
updataHobby() {
// this.student.hobby.splice(0, 1, '开车');
Vue.set(this.student.hobby, 0, '开车')
},
removeSmoke() {
this.student.hobby = this.student.hobby.filter((h) => {
return h !== '抽烟'
})
}

}
});
</script>

</html>


举报

相关推荐

0 条评论