Can I pass parameters in computed properties in Vue.Js

前端 未结 10 1264
借酒劲吻你
借酒劲吻你 2020-11-27 10:38

is this possible to pass parameter in computed properties in Vue.Js. I can see when having getters/setter using computed, they can take a parameter and assign it to a variab

10条回答
  •  甜味超标
    2020-11-27 10:57

    Computed could be consider has a function. So for an exemple on valdiation you could clearly do something like :

        methods: {
            validation(attr){
                switch(attr) {
                    case 'email':
                        const re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
                        return re.test(this.form.email);
                    case 'password':
                        return this.form.password.length > 4
                }
            },
            ...
        }
    

    Which you'll be using like :

      
    

    Just keep in mind that you will still miss the caching specific to computed.

提交回复
热议问题