No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    save method name in variable

    Framework
    2
    4
    84
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Pedro last edited by

      export default {
        data () {
          return {
            methodToCall: ''
          }
        },
        methods: {
          someWhereInCode () {
            this.methodToCall = 'myMethod1'
          },
          someFunction () {
            // execute myMethod1 or myMethod2
            this[methodToCall]() doesn't work
            this.methodToCall() doesn't work
      
          },
          myMethod1 () {
      
          },
          myMethod2 () {
      
          }
        }
      }
      
      

      Hi
      Is it possible to call a method from a variable name ?
      The method to execute depends on the return of a function.
      I tried the way I post in the example code and also declaring a variable on the <script>. No success.

      Is there a way of doing this ?

      Tks

      beets 1 Reply Last reply Reply Quote 0
      • beets
        beets @Pedro last edited by beets

        @Pedro use

        this[this.methodToCall]()
        
        1 Reply Last reply Reply Quote 1
        • P
          Pedro last edited by

          It worked and helped me a lot
          Thanks

          beets 1 Reply Last reply Reply Quote 0
          • beets
            beets @Pedro last edited by

            @Pedro You can also use:

            const methodToCall = this.methodToCall
            this[methodToCall]()
            

            if you didn’t like the double this

            1 Reply Last reply Reply Quote 0
            • First post
              Last post