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

Linux kernel device driver memory management notes

/********************** * Linux memory management...

Solution to the problem of adaptive height and width of css display table

Definition and Usage The display property specifi...

Java programming to write a JavaScript super practical table plug-in

Table of contents Effects Documentation first ste...

Use iframe to submit form without refreshing the page

So we introduce an embedding framework to solve th...

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...

How to install Nginx in CentOS7 and configure automatic startup

1. Download the installation package from the off...

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...

Mysql cannot select non-aggregate columns

1. Introduction I recently upgraded my blog and a...

HTML Tutorial: Ordered Lists

<br />Original text: http://andymao.com/andy...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

Detailed description of the function of new in JS

Table of contents 1. Example 2. Create 100 soldie...

In-depth analysis of the Identifier Case Sensitivity problem in MySQL

In MySQL, you may encounter the problem of case s...