我正在使用Vuetify制作多组件Vue应用程序,并实现了“设置”页面(Settings.vue),并使用v-switch
进行主题设置以将应用程序更改为暗模式。我可以使用v-model="this.$store.state.isDark"
来获得开关的初始状态,但是当我单击它时,我将其设置为运行方法@change="changeDark()"
。
methods
methods: {
changeDark: () => this.$store.dispatch("commitDarkMode")
}
我在控制台中收到此错误
Error in v-on handler: "TypeError: Cannot read property '$store' of null"
Cannot read property '$store' of null
我读到这是因为他们的开关没有包装在v-app
中,但是我的是,这是我的App.vue
<template>
<v-app :dark="this.$store.state.isDark">
<Header />
<router-view />
</v-app>
</template>
还有我的Settings.vue
<template>
<v-main>
<v-container fluid>
<v-row>
<v-col cols="4">
<v-card>
<v-card-title> Worlds - {{this.$store.state.worldsList.length}} </v-card-title>
<v-card-subtitle> List of total saved worlds </v-card-subtitle>
<v-divider></v-divider>
<v-list>
<v-list-item v-for="(n, index) in this.$store.state.worldsList" :key="n + index">
<v-card flat fluid>
<v-card-title> {{n.name}} </v-card-title>
<v-card-subtitle> {{n.desc}} </v-card-subtitle>
</v-card>
</v-list-item>
</v-list>
</v-card>
</v-col>
<v-col cols="6">
<v-card>
<v-card-title>Theme Settings</v-card-title>
<v-divider></v-divider>
<v-switch v-model="this.$store.state.isDark" :label="`Dark Mode`" @change="changeDark()"></v-switch>
<v-card-subtitle> <b> More Coming Soon </b> </v-card-subtitle>
</v-card>
</v-col>
<v-col cols="2">
<v-card>
<b> More Coming Soon </b>
</v-card>
</v-col>
</v-row>
<v-row></v-row>
</v-container>
</v-main>
</template>
我认为这是因为它无法访问Vue实例,因为this
不起作用,但是为什么呢?
编辑:
使用v-btn
可以很好地工作,似乎开关不起作用。我也尝试过v-checkbox
,但它也不起作用
答案 0 :(得分:1)
使用mapActions可以很好地工作,但是我认为问题不在于调度,而在于设置原始值,因为当我更改它时,它已经固定了。
此:
<v-switch v-model="this.$store.state.isDark" :label="Dark Mode" @change="changeDark()"></v-switch>
至
<v-checkbox v-model="darkMode" :label="Dark Mode" @change="changeDark()"></v-checkbox>
答案 1 :(得分:0)
changeDark() {
this.$store.dispatch("commitDarkMode")
}