Detailed explanation of the principle of Vue monitoring data

Detailed explanation of the principle of Vue monitoring data

insert image description here

<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>

Summarize

This 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:
  • Analysis of the reasons why Vue3 uses Proxy to implement data monitoring
  • Detailed explanation of the principle of Vue monitoring data
  • Use of Vue data monitoring method watch
  • Case analysis of data monitoring and data interaction in Vue
  • Detailed explanation of the principle of Vue monitoring data

<<:  Automated front-end deployment based on Docker, Nginx and Jenkins

>>:  Tips on making web pages for mobile phones

Recommend

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

A brief talk about Rx responsive programming

Table of contents 1. Observable 2. Higher-order f...

MySQL select, insert, update batch operation statement code examples

In projects, batch operation statements are often...

Using cursor loop to read temporary table in Mysql stored procedure

cursor A cursor is a method used to view or proce...

Elements of user experience or elements of web design

System and user environment design <br />Th...

Detailed explanation of MySQL precompilation function

This article shares the MySQL precompilation func...

MySQL database constraints and data table design principles

Table of contents 1. Database constraints 1.1 Int...

How to use VLAN tagged Ethernet card in CentOS/RHEL system

In some scenarios, we want to assign multiple IPs...

React-native sample code to implement the shopping cart sliding deletion effect

Basically all e-commerce projects have the functi...

Handtrack.js library for real-time monitoring of hand movements (recommended)

【Introduction】: Handtrack.js is a prototype libra...

Native JS implementation of loading progress bar

This article shares a dynamic loading progress ba...

Detailed explanation of JavaScript's built-in objects Math and strings

Table of contents Math Objects Common properties ...

How to keep running after exiting Docker container

Phenomenon: Run an image, for example, ubuntu14.0...

Docker builds jenkins+maven code building and deployment platform

Table of contents Docker Basic Concepts Docker in...