search

Home  >  Q&A  >  body text

How to access $vuetify instance in setup function

Is there a way to access $vuetify (and any other added globals) in the settings function? Is there a way to give composables access to it?

  ...
  setup() {
    const { isDesktop } = $vuetify.breakpoints.mdAndUp; // <=== how to get $vuetify
    return { isDesktop };
  },


P粉659518294P粉659518294376 days ago1086

reply all(1)I'll reply

  • P粉692052513

    P粉6920525132023-11-18 15:22:30

    Can be combined to obtain vuetify instances:

    // useVuetify.ts
    import { getCurrentInstance } from 'vue'
    
    export function useVuetify() {
      const instance = getCurrentInstance()
      if (!instance) {
        throw new Error(`useVuetify should be called in setup().`)
      }
      return instance.proxy.$vuetify
    }
    

    Import it into your component:

    <!-- MyComponent.vue -->
    <script lang="ts">
    import { useVuetify } from './useVuetify'
    import { computed } from 'vue'
    
    /*...*/
      setup() {
        const vuetify = useVuetify()
        const isDesktop = computed(()=>vuetify.breakpoints.mdAndUp)
        return { isDesktop }
      },
    /*...*/
    </script>
    

    If you are using Vue <= 2.6.14 + @vue/composition-api instead of Vue 2.7, please replace 'vue' with '@vue /composition-api'

    reply
    0
  • Cancelreply