How to access $vuetify instance in setup function
P粉659518294
P粉659518294 2023-11-17 17:47:45
0
1
1084

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粉659518294
P粉659518294

reply all(1)
P粉692052513

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 @vue/composition-api instead of Vue 2.7, please replace 'vue' with '@vue /composition-api'

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template