1. ホーム
  2. Web プログラミング
  3. ジャバスクリプト
  4. javascriptのクラスライブラリ

Vueの「データを聴く」原則を解説

2022-01-13 12:49:20

画像

<body>
    <div id="root">
        <h1>Basic information about the student</h1>
        <button @click="student.age++">age+1 years</button>
        <button @click="addSex">Add gender property default value is male</button><br>
        <button @click="student.sex='unknown' ">Modify attribute value </button><br>
        <button @click="addFriend">Add a friend right at the top of the list</button><br>
        <button @click="updateFriend">Update the name of the first person</button><br>
        <button @click="addHobby">Add a hobby</button><br>
        <button @click="change">Modify the first hobby to climbing</button><br>
        <button @click="removeSmoke">Filter out smoking</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: ['drink', 'smoke', 'perm'],
                    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 = "小刘";
                    this.student.friends[0].age = 22
                },
                addHobby() {
                    this.student.hobby.push('singing')
                },
                change() {
                    //splice add means start with the 0th and delete one, the new value added is climbing
                    //Note: you can't modify directly by array subscript form 
                    //this.student.hobby.splice(0, 1, 'climbing')
                    //Vue.set(this.student.hobby, 0, 'climbing')
                    this.$set(this.student.hobby, 0, 'climbing')
                },
                removeSmoke() {
                    //filter does not affect changes to the original array
                    this.student.hobby = this.student.hobby.filter((h) => {
                        return h ! == 'smoking'
                    })
                }
            }
        })
    </script>
</body>

概要

この記事はこれで終わりです。この記事があなたの助けになることを願っていますし、BinaryDevelopのより多くのコンテンツにもっと注意を払うことを願っています