Skip to content

计算属性(computed)和侦听器(watch)

设计者可以在脚本中按照Vue计算属性和侦听器使用方式,对数据进行处理。

示例脚本

js
{
    props: {
        firstName: {
            type: String
        }
    },
    data: function () {
        return {
            lastName:'张三',
            fullname:null,
        }
    },
    watch:{ 
        lastName:function(){ 
            this.fullname = this.firstName +'.' + this.lastName; 
        }, 
        descs:{ 
            handler: function(){ 
                this.fullname = this.firstName +'.' + this.lastName; 
            }, 
            immediate: true
        }, 
    } 
    computed:{ 
        compFullName(){ 
            return this.firstName +'.' + this.lastName; 
        }, 
    }, 
    created: function () {
        console.log('script.created');
    },
    mounted: function () {
        console.log('script.mounted');
        var that = this;
        setTimeout(function () {
            that.setup();
        }, 0);
    },
    beforeDestroy: function () {

    },
    methods: {
        setup: function () {
            var that = this;
            console.log('script.setup');
            that.setFormDesc(this.descs);
        },
        setFormDesc: function(val) {
            var that = this;
            console.log('script.setFormDesc');
            that.form.descs = 'props:' + (val == null ? '' : val);
        }
    }
}