So greifen Sie in der Setup-Funktion auf die $vuetify-Instanz zu
P粉659518294
P粉659518294 2023-11-17 17:47:45
0
1
1176

Gibt es eine Möglichkeit, in der Einstellungsfunktion auf $vuetify (und alle anderen hinzugefügten Globals) zuzugreifen? Gibt es eine Möglichkeit, Composables Zugriff darauf zu gewähren?

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


P粉659518294
P粉659518294

Antworte allen(1)
P粉692052513

可组合获取 vuetify 实例:

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

将其导入您的组件中:

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

如果您使用的是 Vue @vue/composition-api 而不是 Vue 2.7,请将 'vue' 替换为 '@vue /composition-api'

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage