【问题标题】:How to get Vuetify to update its colours when you change the theme programatically以编程方式更改主题时如何让 Vuetify 更新其颜色
【发布时间】:2020-09-16 08:30:57
【问题描述】:

我无法以编程方式更新主题。它会更新,但需要刷新才能显示主题更新。我试过使用 location.reload();在更新时刷新页面。这行不通。

我使用 Vue 构建应用程序。

我目前正在将我的商店导入 vuetify 插件。

import Vue from "vue";
import Vuetify from "vuetify/lib";
import store from "./../store";

Vue.use(Vuetify);

let theme = store.getters.THEME;

export default new Vuetify({
  theme: {
    themes: {
      light: {
        primary: theme.primary,
        secondary: theme.secondary,
        accent: theme.accent,
        error: theme.error
      }
    }
  }
});

我设置了使用 axios 从服务器返回的主题变量。

setTheme() {
  this.$http
    .get(`organisations/${this.organisation}`)
    .then(response => {
      this.$store.dispatch("SET_THEME", response.data.organisation.theme);
    })
    .catch(e => {
      this.errors.push(e);
    });
}

一切正常,甚至奇怪的是我存储在同一个 vuex 状态对象中的徽标更新。

state: {
    auth: {},
    user: {},
    organisation: {},
    theme: {
      primary: "#00ACC1",
      secondary: "#84FFFF",
      accent: "#80CBC4",
      error: "#EF9A9A",
      logo: ""
    },
    settings: {},
    rules: [],
    rulesFinal: {},
    journey: "main",
    selectedTable: {},
    selectedOrder: {},
    selectedCategory: {}
},

但是,即使同一对象中的徽标动态更新,我设置的颜色也不会。是否有我遗漏的东西或某种强制 vuetify 更新的方法?

我现在尝试了三种不同的方法,如果能得到任何帮助,我将不胜感激。

【问题讨论】:

    标签: vue.js vuex vuetify.js


    【解决方案1】:

    我最终直接在响应中设置了值。

    setTheme() {
          const payload = {
            organisation: this.organisation
          };
          setTheme(payload)
            .then(response => {
              const theme = this.$vuetify.theme.themes.light;
              const responseTheme = response.organisation.theme;
              theme.primary = responseTheme.primary_color;
              theme.secondary = responseTheme.secondary_color;
              theme.accent = responseTheme.accent_color;
              theme.error = responseTheme.error_color;
              this.$store.dispatch("SET_THEME", response.data.organisation.theme);
            })
            .catch(e => {
              this.errors.push(e);
            });
        },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 2014-01-30
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2016-07-03
      相关资源
      最近更新 更多