有没有办法在设置函数中访问 $vuetify (以及任何其他添加的全局)? 有没有办法让可组合项访问它?
... setup() { const { isDesktop } = $vuetify.breakpoints.mdAndUp; // <=== how to get $vuetify return { isDesktop }; },
可组合获取 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'
'vue'
'@vue /composition-api'
可组合获取 vuetify 实例:
将其导入您的组件中:
如果您使用的是 Vue @vue/composition-api 而不是 Vue 2.7,请将
'vue'
替换为'@vue /composition-api'