在Vuetify中使用自定义主题并将颜色变量传递给组件

时间:2018-01-16 12:12:31

标签: vuejs2 vue-component vuetify.js

在我的index.js文件中,我手动使用我公司的颜色覆盖了Vuetify theme对象:

Vue.use(Vuetify, {
  theme: {
    primary: '#377ef9',
    secondary: '#1b3e70',
    accent: '#ff643d',
    error: '#ff643d'
    ...
  }

现在,我可以使用我的模板中的这些颜色:

<my-text-field name="input text"
    label="text"
    value="text text text text..."
    type="text"
    color="primary">
</my-text-field>

在我的模板样式中使用primary或上面定义的theme对象中的任何其他变量之后我所做的:

<script>
  import { VTextField } from 'vuetify'
  export default {
    extends: VTextField
  }
</script>

<style scoped lang="stylus">
  label
    color: <seconday color> <-- this is what I'm after
    color: #1b3e70 <-- this works, but not quite good enough for me
</style>

我可以轻松地在样式部分中编写我的颜色的十六进制值,但我不想重复自己,宁愿使用我的theme对象,这样对我来说也会更容易轻松改变各处的颜色,避免拼写错误导致颜色定义出错。

6 个答案:

答案 0 :(得分:19)

编辑(2018/10/11)

从版本1.2.开始,我们可以启用CSS变量
注意:据称它无法在IE中运行(Edge应该可以使用),可能还有一些Safari版本?

来自docs(请参阅自定义属性

  

启用customProperties还会为每个生成一个css变量   主题颜色,然后您可以在组件中使用   块。

Vue.use(Vuetify, {
  options: {
    customProperties: true
  }
})

<style scoped>
  .something {
    color: var(--v-primary-base)
    background-color: var(--v-accent-lighten2)
  }
</style>

对于自定义值,例如
yourcustomvariablename: '#607D8B'
使用--v-yourcustomvariablename-base(因此默认为base)。

<小时/>

原始答案:

github上有Feature Request:在手写笔文件中访问主题颜色

@KaelWD(开发者之一)wrote

  

这是你必须自己实施的。我试过了   之前类似的东西,但它并没有真正起作用于框架   水平。

问题标记为wontfix

<小时/> 编辑(2018/10/11)
另见这个更新的主题:
https://github.com/vuetifyjs/vuetify/issues/827(功能请求:原生css变量)

答案 1 :(得分:15)

有一种方法可以利用:style属性解决这个问题。它可用于反应性地设置自定义CSS属性。

添加计算属性:

computed: {
    cssProps () {
        return {
            '--secondary-color': this.$vuetify.theme.secondary
        }
    }

将样式绑定到cssProps:

<div id="app" :style="cssProps">

然后,按照你的风格:

<style scoped>
    label
        color: var(--secondary-color);
</style>

改编自此讨论:https://github.com/vuejs/vue/issues/7346

答案 2 :(得分:7)

对于从Vuetify V2开始遇到麻烦的人,您可以执行以下操作来访问SCSS颜色变量。

// Import the Vuetify styles somewhere global
@import '~vuetify/src/styles/styles.sass';

// Now in your components you can access the colour variables using map-get
div {
  background: map-get($grey, lighten-4);
}

所有颜色都可以在/node_modules/vuetify/styles/settings/_colors.scss中找到。

答案 3 :(得分:2)

切换主题(helpfull link)的示例:

    <v-app :dark="setTheme" 
           :style="{background: $vuetify.theme.themes[theme].background}" 
           >

JS:

      computed: {
            setTheme() {
                this.$vuetify.theme.dark = this.goDark;
            }
      },
      data() {
            return { 
                goDark: false
            }
      }

答案 4 :(得分:0)

从以上答案中,如果您想包括所有vuetify颜色,请将此代码放入App.vue模板中

<v-app :style="cssProps">

App.vue脚本

computed: {
   cssProps () {
      var themeColors = {}
      Object.keys(this.$vuetify.theme.themes.light).forEach((color) => {
        themeColors[`--v-${color}`] = this.$vuetify.theme.themes.light[color]
      })
      return themeColors
   }
}

让我们说一下vuetify.js中是否有这种颜色

export default new Vuetify({
  treeShake: true,
    theme: {
      themes: {
        light: {
          darkRed: "#CD3300",
        }
      }
    }
})

然后,在任何组件中:

<style scoped>
  .label {
    color: var(--v-darkRed);
  }
</style>

答案 5 :(得分:0)

也许我迟到了最有效的方法是文档中提到的https://vuetifyjs.com/en/features/theme/#custom-properties

我将为此提供一个工作示例。 您只需进行三项更改即可使其生效。

  1. 提及具有魔力的选项和您的主题颜色
export default new Vuetify({
    theme: {
        options: {
            customProperties: true
        },
        themes: {
            light: {
                primary: "#3DCFD3",
                secondary: "#171b34",
                accent: "3D87E4"
            }
        }
    }
});
  1. 在要应用主题的标签中提及类名
<h4 class="blue-header">Yash Oswal</h4>  
  1. CSS 来应用您的主题。
<style lang="scss">

.blue-header {
  color: var(--v-primary-base);
}

</style>