Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameter in Vue.js

I have below method in vuejs

methods: {
    show () {
    }
}

In this show() method often I would like to pass parameter, often I would not like to pass parameter when I'll call it HTML. Like show (obj) and show ()

How can I do that ?

Thanks

like image 988
abu abu Avatar asked Oct 22 '25 19:10

abu abu


2 Answers

methods: {
    show (obj) {
        if(obj) {
            // code to be executed if obj was passed
        } else {
            // code to be executed if no obj was passed
        }
        // code to be executed regardless 
    }
}
like image 152
connexo Avatar answered Oct 25 '25 11:10

connexo


You could also set a optional value by using a function parameter default value. See this vuejs example

like image 41
ScottyG Avatar answered Oct 25 '25 09:10

ScottyG