Can I pass parameters in computed properties in Vue.Js

前端 未结 10 1217
借酒劲吻你
借酒劲吻你 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 11:00

    Yes methods are there for using params. Like answers stated above, in your example it's best to use methods since execution is very light.

    Only for reference, in a situation where the method is complex and cost is high, you can cache the results like so:

    data() {
        return {
            fullNameCache:{}
        };
    }
    
    methods: {
        fullName(salut) {
            if (!this.fullNameCache[salut]) {
                this.fullNameCache[salut] = salut + ' ' + this.firstName + ' ' + this.lastName;
            }
            return this.fullNameCache[salut];
        }
    }
    

    note: When using this, watchout for memory if dealing with thousands

提交回复
热议问题