欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

计算属性的getter和setter

程序员文章站 2024-02-20 15:45:40
...
<!DOCTYPE html>
<html>
<head>
    <title>计算属性的getter和setter</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id='app'>
        {{fullName}}
    </div>

<script>
    var vm =  new Vue({
        el:'#app',
        data: {
            firstName: "Dell",
            lastName: "Dll"
        },
        computed: {
            fullName: {
                get: function() {
                    return this.firstName + " " + this.lastName;
                },
                set: function(vaule) {
                    var arr = vaule.split(" ");
                    this.firstName = arr[0];
                    this.lastName = arr[1];
                }
            }
        }
    })
</script>
</body>
</html>