如何在设置 Vuetify 3 中使用全局变量

Mey*_*ron 1 vue.js vuetify.js vuejs3 vue-composition-api vuetifyjs3

我使用 vue 3 设置,我想访问我使用的全局变量 vuetify。\n我可以在 config.globalVariable 中看到它,但我无法在设置中使用它。

\n
{_uid: 0, _component: {\xe2\x80\xa6}, _props: null, _container: div#app, _context: {\xe2\x80\xa6}, \xe2\x80\xa6}\ncomponent: \xc6\x92 component(name, component)\nconfig: Object\ncompilerOptions: (...)\nerrorHandler: undefined\nglobalProperties:\n$vuetify: {defaults: {\xe2\x80\xa6}}\n$messageSystem: {addMessage: \xc6\x92}\n__proto__: Object\nisNativeTag: (tag) => {\xe2\x80\xa6}\n...\n
Run Code Online (Sandbox Code Playgroud)\n

如您所见,我看到了 $vuetify,但在设置中我无法访问它。

\n
    setup() {\n       ...\n       this.$vuetify.theme.dark = true; \n       ...\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是我访问全局变量的方式吗?

\n

我得到的错误:\nCannot read property \'$vuetify\' of undefined

\n

Bou*_*him 5

您无法访问挂钩this内部setup,但可以使用它getCurrentInstance来获取包含配置的应用程序实例:

import { getCurrentInstance,onMounted } from 'vue'

export default {
  setup() {
    const internalInstance = getCurrentInstance()

    onMounted(()=>{
      nternalInstance.appContext.config.globalProperties.$vuetify.theme.dark=true
   })
  }
Run Code Online (Sandbox Code Playgroud)