简体   繁体   中英

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

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 
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM