vue.js中的computed计算属性

<script>
    var vm = new Vue({
        el: '#app',
        data: {
            name: 'Google',
            url: 'http://www.google.com'
        },
        computed: {
            site: {
                // getter
                get: function () {
                    return this.name + ' ' + this.url
                },
                // setter
                set: function (newValue) {
                    var names = newValue.split(' ')
                    this.name = names[0]
                    this.url = names[names.length - 1]
                }
            }
        }
    })
    // 调用 setter, vm.name 和 vm.url 也会被对应更新
    vm.site = '百度 http://www.baidu.com';//对computed中的对应属性赋值的时候,会自动调用属性中的set()方法,并将赋的值作为参数传递给set()函数
    document.write('name: ' + vm.name);
    document.write('<br>');
    document.write('url: ' + vm.url);
</script>

猜你喜欢

转载自blog.csdn.net/shj_php/article/details/88039969