vue学习10: 计算属性的getter和setter

直接上代码:
<div id="app">
        <span>{{fullName}}</span>
    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                firstName:'Dell',
                lastName: 'Lee'
            },
            computed: {
                fullName:{
                    get: function () {
                        return this.firstName + " " + this.lastName;
                    },
                    set: function (value) {
                       var arr = value.split(" ");
                       this.firstName = arr[0];
                       this.lastName = arr[1];
                    }
                }
            }
        })
    </script>


1、get用来获取属性值。同时计算生成新的值。

2、set:用来改变船体过来的值,发生改变的时候,同时让数据的内容也发生改变。

猜你喜欢

转载自blog.csdn.net/weixin_40814356/article/details/80158162