在vuejs模板中使用样式标签并从数据模型更新

For*_*Dev 12 javascript vue.js vuejs2

我想动态更新样式inside样式标签.

但是,为Vue创建容器模型会删除style标记.我知道样式标签应该属于页面的头部,但这只是为了方便使用.

所以我想要的是一个包含元素和样式标签的包装器:

<div class="setting">
  <style>
    .setting input {
      background: {{bgColor}};
    }
  </style>
  <input class="setting" type="text" v-model="bgColor">
</div>
Run Code Online (Sandbox Code Playgroud)

输入的值应更新css样式的值.每当使用简单的div元素时,这都有效,但样式标签似乎是个问题

javascript设置如下:

new Vue({
    el: '.setting',
    data: {
      bgColor: 'red'
    }
});
Run Code Online (Sandbox Code Playgroud)

但是,当样式标记具有特定的id时,这可能有效,但我无法将其绑定到输入字段.

<style id="setting">
  #blue {
    background: {{bg}}
  }
  #blue:hover {
    background: {{bgHover}}
  }
</style>

<div id="blue"></div>
Run Code Online (Sandbox Code Playgroud)

和js:

new Vue({
    el: '#setting',
    data: {
      bg: 'blue',
      bgHover: 'red'
    }
});
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我理解如何在样式标签之间更新值. jsfiddle成立

谢谢.

Tro*_*use 25

vue-loader(和 Vue 模板编译器Vue.compile(..))都会过滤掉<style>template.

解决这个问题的一个简单解决方案是利用 Vue 的内置<component>组件

<template>
  <div>
    <component is="style">
      .foo[data-id="{{ uniqueId }}"] {
        color: {{ color }};
      }
      .foo[data-id="{{ uniqueId }}"] .bar {
        text-align: {{ align }}
      }
    </component>
    <div class="foo" :id="id" :data-id="uniqueId">
      <div class="bar">
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    id: {
      type: String,
      default: null
    }
  },
  computed: {
    uniqueId() {
      // Note: this._uid is not considered SSR safe though, so you
      // may want to use some other ID/UUID generator that will
      // generate the same ID server side and client side. Or just
      // ensure you always provide a unique ID to the `id` prop
      return this.id || this._uid;
    },
    color() {
      return someCondition ? 'red' : '#000';
    },
    align() {
      return someCondition ? 'left' : 'right';
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

需要一个唯一的 ID(或其他一些数据属性)来将样式“范围”到这个组件。

这是一个很好的解决方案,因为您可以v-for根据需要使用循环来生成样式内容(这可以对组件数据/道具/计算道具的变化做出反应)

<component is="style" type="text/css">
  <template v-for="item in items">
    [data-id="{{ uniqueId }}"] div.bar[data-div-id="item.id"]::before {
      content: "{{ item.content }}";
      color: {{ item.color }};
    }
  </template>
</component>
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方法!我不得不使用 `&lt;component is="'style'"&gt;` 来阻止 VS Code 快乐 (2认同)

acd*_*ior 22

这是我认为一个好的解决方法/解决方案.

它只是一个自定义组件,因此它可以重复使用.所有Vue的商品v-if都可以使用.

另一个专业是,只要组件生成,生成的样式就会存在!

Vue.component('v-style', {
  render: function (createElement) {
    return createElement('style', this.$slots.default)
  }
});


// demo usage, check the template
new Vue({
  el: '#app',
  data: {
    bgColor: 'red'
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue"></script>

<div id="app" class="stuff">
  <v-style>
    .stuff input {
      background: {{bgColor}};
    }
  </v-style>

  Remove "red" and type "yellow":
  <input class="setting" type="text" v-model="bgColor">
</div>
Run Code Online (Sandbox Code Playgroud)

我看到的一个缺点是,由于标签的名称是<v-style>(或者你选择调用它的任何东西)而不是<style>,IDE可能不会很好地着色它.但除此之外,它就像一个普通的<style>标签.


标准解决方案:使用 v-bind:style

这不会修改style标签,但设置样式的标准方法是使用对象样式绑定.

基本上,您使用:style属性并以对象的形式为其指定样式的CSS属性.演示如下.

new Vue({
  el: '.setting',
  data: {
    bgColor: 'red'
  },
  computed: {
    inputStyles() {
      return {
        background: this.bgColor
      }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue"></script>

<div class="setting">
  Remove "red" and type "yellow":
  <input class="setting" type="text" v-model="bgColor" :style="inputStyles">
</div>
Run Code Online (Sandbox Code Playgroud)