vue.js - 如何在设置 Vuetify 3 中使用全局变量
问题描述
我使用 vue 3 设置,我想访问我使用的全局变量 vuetify。我可以在 config.globalVariable 中看到它,但我不能在设置中使用它。
{_uid: 0, _component: {…}, _props: null, _container: div#app, _context: {…}, …}
component: ƒ component(name, component)
config: Object
compilerOptions: (...)
errorHandler: undefined
globalProperties:
$vuetify: {defaults: {…}}
$messageSystem: {addMessage: ƒ}
__proto__: Object
isNativeTag: (tag) => {…}
...
如您所见,我看到了 $vuetify,但在设置中我无法访问它。
setup() {
...
this.$vuetify.theme.dark = true;
...
这是我访问全局变量的方式吗?
我得到的错误:
Cannot read property '$vuetify' of undefined
解决方案
您无法访问钩子this
内部,但您可以使用它来获取包含配置的应用程序实例:setup
getCurrentInstance
import { getCurrentInstance,onMounted } from 'vue'
export default {
setup() {
const internalInstance = getCurrentInstance()
onMounted(()=>{
nternalInstance.appContext.config.globalProperties.$vuetify.theme.dark=true
})
}
推荐阅读
- html - 如何使弹出位置相对于整个页面而不是触发它的按钮
- intellij-idea - PyCharm git commit 拆分代码块更改
- gcc - 为什么有些 C 编译器允许在文件范围内使用 __func__?
- python - xml.etree.ElementTree root.findall 不返回所有
- raspberry-pi - 如何将鼠标连接到树莓派上的蓝牙
- python - Numpy:基于索引列表访问多维数组的值
- c# - 重命名的 WPF exe 抛出 FileNotFoundException(不典型)
- java - Java 应用程序不请求使用麦克风的权限 (MacOs Big Sur)
- css - 如何使用 flexbox 动态地进行 3 列 3 行的布局
- r - 分析在 R 中具有特定主题标签或关键字的推文中使用的表情符号