計算屬性(computed)的getter和setter

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
    <div id="app">{{fullName}}</div>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#app",
            data: {
                firstName: "Tom",
                lastName: "Cat"
            },
            computed: {
                /*fullName: function() {
                    return this.firstName + " " + this.lastName
                }*/
                fullName: {
                    get: function() {
                        return this.firstName + " " + this.lastName
                    },
                    set: function(value) {
                        var arr = value.split(" ");
                        this.firstName = arr[0];
                        this.lastName = arr[1];
                        console.log("我是" + value)
                    }
                }
            }
        })
    </script>
</body>
</html>

計算屬性(computed)的getter和setter

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章