Vuetify 在明暗主题之间切换(使用 vuex) [英] Vuetify toggle between light and dark theme (with vuex)
问题描述
所以在我的 Vue 项目中,我基本上有两个页面/组件,它们将根据 url 使用 vue-router 显示.我可以通过一个按钮在这些页面/组件之间切换.
我也在使用 vuex 来管理一些数据.
现在我在其中一个组件中加入了一个开关,用于在 vuetify 的深色和浅色主题之间切换.在这个组件的模板中,我做了:
在被调用的函数中,我这样做:
toggleDarkTheme() {this.$vuetify.theme.dark = !this.$vuetify.theme.dark;console.log(this.$vuetify.theme.dark);},
在 app.vue 中我包含了 <v-app dark>
并且在我的 main.js 中我有以下内容:
Vue.use(Vuetify);const vuetify = 新 vuetify({主题: {//暗:真,主题:{光: {主要:colors.purple,次要:colors.grey.darken1,口音:colors.shades.black,错误:colors.red.accent3,背景:colors.indigo.lighten5,},黑暗的: {主要:colors.blue.lighten3,背景:colors.indigo.base,},},},});Vue.config.productionTip = false;新的 Vue({路由器,店铺,验证,渲染:(h) =>h(应用程序),}).$mount('#app');
所以我现在的问题是,当我单击开关时,该组件中的主题确实从亮模式切换到暗模式.浅色模式是默认模式,当我点击开关一次时,我会得到深色主题.但是,当我单击按钮切换到另一个 url 时,将使用浅色主题.如何正确实现此功能?
提前致谢!
你应该有一个名为 vuetify.js
的 JavaScript 类,在你的情况下应该是这样的.
从vue"导入Vue;从vuetify/lib"导入 Vuetify;Vue.use(Vuetify);导出默认的新 Vuetify({主题: {主题:{光: {主要:colors.purple,次要:colors.grey.darken1,口音:colors.shades.black,错误:colors.red.accent3,背景:colors.indigo.lighten5},黑暗的: {主要:colors.blue.lighten3,背景:colors.indigo.base}}}});
你的开关应该可以工作,但为了以防万一,试试我在你的组件中制作的这个按钮.
<v-tooltip v-if="!$vuetify.theme.dark" 底部><template v-slot:activator="{ on }"><v-btn v-on="on" color="info" small fab @click="darkMode"><v-icon class="mr-1">mdi-moon-waxing-crescent</v-icon></v-btn>模板><span>暗模式开启</span></v-工具提示><v-tooltip v-else 底部><template v-slot:activator="{ on }"><v-btn v-on="on" color="info" small fab @click="darkMode"><v-icon color="yellow">mdi-white-balance-sunny</v-icon></v-btn>模板><span>暗模式关闭</span></v-工具提示>
按钮在您的
darkMode() {this.$vuetify.theme.dark = !this.$vuetify.theme.dark;}
so in my Vue-project I basically have two pages/components that will be shown with the use of vue-router depending on the url. I can switch between those pages/components via a button.
I am also using vuex for the management of some data.
Now I included a switch in one of the components to toggle between a dark and a light theme from vuetify. In the template for this component I do:
<v-switch
label="Toggle dark them"
@change="toggleDarkTheme()"
></v-switch>
And in the function that gets called I do:
toggleDarkTheme() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark;
console.log(this.$vuetify.theme.dark);
},
In app.vue I have included the <v-app dark>
and in my main.js i have the following:
Vue.use(Vuetify);
const vuetify = new Vuetify({
theme: {
// dark: true,
themes: {
light: {
primary: colors.purple,
secondary: colors.grey.darken1,
accent: colors.shades.black,
error: colors.red.accent3,
background: colors.indigo.lighten5,
},
dark: {
primary: colors.blue.lighten3,
background: colors.indigo.base,
},
},
},
});
Vue.config.productionTip = false;
new Vue({
router,
store,
vuetify,
render: (h) => h(App),
}).$mount('#app');
So my problem now is, when I click the switch, the theme is indeed toggled from light to dark mode in this component. Light mode is the default and when i click the switch once, I get dark theme. However when i click the button to switch to the other url, there the light theme will be used. How do I implement this feature correctly?
Thanks in advance!
You should have a JavaScript class called vuetify.js
, that should look like this in your case.
import Vue from "vue";
import Vuetify from "vuetify/lib";
Vue.use(Vuetify);
export default new Vuetify({
theme: {
themes: {
light: {
primary: colors.purple,
secondary: colors.grey.darken1,
accent: colors.shades.black,
error: colors.red.accent3,
background: colors.indigo.lighten5
},
dark: {
primary: colors.blue.lighten3,
background: colors.indigo.base
}
}
}
});
Your switch should be working, but just in case, try this button I've made in your component.
<div>
<v-tooltip v-if="!$vuetify.theme.dark" bottom>
<template v-slot:activator="{ on }">
<v-btn v-on="on" color="info" small fab @click="darkMode">
<v-icon class="mr-1">mdi-moon-waxing-crescent</v-icon>
</v-btn>
</template>
<span>Dark Mode On</span>
</v-tooltip>
<v-tooltip v-else bottom>
<template v-slot:activator="{ on }">
<v-btn v-on="on" color="info" small fab @click="darkMode">
<v-icon color="yellow">mdi-white-balance-sunny</v-icon>
</v-btn>
</template>
<span>Dark Mode Off</span>
</v-tooltip>
</div>
The Button calls this method in your <script>
darkMode() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark;
}
这篇关于Vuetify 在明暗主题之间切换(使用 vuex)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!