Search code examples
vue.jsnuxt.jsdropdown

Nuxtjs buttom DropDown


I'm building a buttom like Dropdown, this button is for print and dowloand document, so this is my code:

<template>
            <v-menu
              transition="slide-y-transition"
              bottom 
              offset-y
            >
              <template v-slot:activator="{ on, attrs }" >
                <v-btn
                  v-bind="attrs"
                  v-on="on"
                >
                  Actions
                </v-btn>
              </template>
              <v-list>
                <v-list-item
                  v-for="(item, index) in actions"
                  :key="index"
                  @click="item.to" 
                >
                  <v-list-item-title class="white--text">
                    {{item.title}}
                  </v-list-item-title>
                </v-list-item>
              </v-list>
            </v-menu>
</template>

<script>

export default {

    data()=>({
    actions: [
      {
        title: 'Print',
        to: 'print()'
      },
      {
        title: 'Download',
        to: 'download()'
      }       
    ]
    }),

    methods: {
      print() {
       //some code    
      },
      download() {
       //some code    
      }
    }

</script>

so, when a try to click in button and select opction "Print" this error is showing me:

TypeError: handler.apply is not a function

why this error is showing me??

is possible call one function like string??


Solution

  • Give the method names without () then add a handler method for the click event as follows :

     @click="handleMethod(item.to)"
    

    and in script :

    export default {
    
        data()=>({
        actions: [
          {
            title: 'Print',
            to: 'print'
          },
          {
            title: 'Download',
            to: 'download'
          }       
        ]
        }),
    
        methods: {
          print() {
           //some code    
          },
          download() {
           //some code    
          },
         handleMethod(method){
           this[method]()
          }
        }
    }
    

    or just remove the () from the method names and try out @click="this[item.to]()"