<body> <div id="root"> <h1>Student's basic information</h1> <button @click="student.age++">Age+1 year old</button> <button @click="addSex">Add gender attribute. The default value is male</button><br> <button @click="student.sex='unknown' ">Modify property value</button><br> <button @click="addFriend">Add a friend at the top of the list</button><br> <button @click="updateFriend">Update the first person's name</button><br> <button @click="addHobby">Add a hobby</button><br> <button @click="change">Change the first hobby to mountain climbing</button><br> <button @click="removeSmoke">Filter out smoke</button><br> <h3>Name: {{student.name}}</h3> <h3>Age: {{student.age}}</h3> <h3 v-if="student.sex">Gender: {{student.sex}}</h3> <h3>Hobbies:</h3> <hr> <ul> <li v-for="(h,index) in student.hobby" :key="index">{{h}}</li> </ul> <hr> <h3>Friends:</h3> <ul> <li v-for="(f,index) in student.friends" :key="index">{{f.name}}--{{f.age}}</li> </ul> </div> <script> Vue.config.productionTip = false; const vm = new Vue({ el: "#root", data: { student: name: 'zhang', age: 18, hobby: ['drinking', 'smoking', 'perming'], friends: [{ name: 'li', age: 15 }, { name: 'wang', age: 10 }] } }, methods: { addSex() { this.$set(this.student, 'sex', 'male') // Vue.set(vm.student, 'sex', 'male') }, addFriend() { this.student.friends.unshift({ name: 'YY', age: 66 }) }, updateFriend() { this.student.friends[0].name = "Xiao Liu"; this.student.friends[0].age = 22 }, addHobby() { this.student.hobby.push('singing') }, change() { //splice addition means starting from the 0th one, deleting one, and the newly added value is climbing //Note: It cannot be modified directly through the array subscript form //this.student.hobby.splice(0, 1, 'climbing') //Vue.set(this.student.hobby, 0, 'climbing') this.$set(this.student.hobby, 0, 'mountain climbing') }, removeSmoke() { //filter does not affect the change of the original array this.student.hobby = this.student.hobby.filter((h) => { return h !== 'smoking' }) } } }) </script> </body> SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Automated front-end deployment based on Docker, Nginx and Jenkins
>>: Tips on making web pages for mobile phones
/********************** * Linux memory management...
Definition and Usage The display property specifi...
Table of contents Effects Documentation first ste...
So we introduce an embedding framework to solve th...
<br />First think of the idea, then draw a s...
Vue+js realizes the fade in and fade out of the v...
1. Download the installation package from the off...
①. How to use the alias (CNAME) record: In the do...
Tomcat's default log uses java.util.logging, ...
1. Introduction I recently upgraded my blog and a...
<br />Original text: http://andymao.com/andy...
There are many tags in XHTML, but only a few are ...
nginx (engine x) is a high-performance HTTP and r...
Table of contents 1. Example 2. Create 100 soldie...
In MySQL, you may encounter the problem of case s...