【问题标题】:Understanding Vue.js CSS Class Binding Ordering了解 Vue.js CSS 类绑定顺序
【发布时间】:2019-07-27 13:05:02
【问题描述】:

谁能帮我理解如何控制组件根元素 css 类和任何可能从调用组件的父级绑定的 css 类的顺序?

这是一个描述我注意到的东西的小提琴(下面的 sn-p 示例): https://jsfiddle.net/cicsolutions/b6rnaw25/

您会注意到,如果您的组件的根元素上有一个类,如果该类是一个字符串,Vue 的类绑定将该类放在生成的绑定类列表的开头。这是我所期望的,因为组件设置了基础 css 类,然后您可以在使用组件时通过向组件 html 元素添加类来自定义样式。然后 Vue 将这些类绑定/连接在一起。

在小提琴的下一个示例中,我将展示动态的 css 类的使用(即不是静态字符串)。在这些情况下,Vue 将组件的根元素类放在绑定类列表的末尾。

我正在开发一个希望其他人使用的组件,所以我想在根元素上设置我的组件类,然后如果有人想覆盖这些样式,他们可以添加自己的类组件标签。

我还需要根元素类是动态的,所以我必须使用数组或对象来处理类绑定。

有谁知道为什么 Vue 将组件根 css 类放在静态类的开头和动态类的末尾?这对我来说似乎很奇怪,但也许是出于我无法理解的原因而故意这样做的。

尽管如此,当我需要它成为动态类时,我将如何确保组件的根元素类始终在结果绑定类列表中排在第一位?

Vue.directive('bound-class', (el) => {
	const boundClass = el.attributes.class.nodeValue
  const boundClassPrintout = document.createElement('div')
  boundClassPrintout.innerHTML = 'Resulting Bound Class: ' + boundClass
  el.appendChild(boundClassPrintout)
});

// STATIC CSS CLASS -> becomes 1st class in bound class list (expected)
Vue.component('string-test', {
	template: `<div class="string-class" v-bound-class><slot></slot></div>`
});

// DYNAMIC CSS CLASS -> becomes last class in bound class list (unexpected)
Vue.component('array-test', {
	template: `<div :class="['array-class']" v-bound-class><slot></slot></div>`
});

// DYNAMIC CSS CLASS -> becomes last class in bound class list (unexpected)
Vue.component('object-test', {
	template: `<div :class="{ 'object-class': true }" v-bound-class><slot></slot></div>`
});

new Vue({
  el: "#app",
  computed: {
  	vueVersion() {
    	return Vue.version
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
}

h2 {
  margin-bottom: 0.75rem;
}
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <h2>Vue version: {{ vueVersion }}</h2>
  <string-test class="p-2 mb-2 border">Root class (string-class) at beginning (expected)</string-test>
  <array-test class="p-2 mb-2 border">Root class (array-class) at end (unexpected)</array-test>
   <object-test class="p-2 mb-2 border">Root class (object-class) at end (unexpected)</object-test>
</div>

【问题讨论】:

  • 1.你的小提琴链接坏了。 2. 如果我理解了这个问题,您正在尝试控制添加到元素的 CSS 类的顺序。那正确吗?如果是这样,为什么?类的顺序没有区别。
  • 小提琴链接已修复,感谢您的关注!是的,您正确理解了这个问题。我也很好奇为什么 Vue 会根据你设置它们的方式来不同地处理这些类。 Vue 是如此聪明,所以可能有一个很好的理由。 CSS 类级联,所以顺序绝对会有所作为。
  • 恐怕你对 CSS 级联的理解是有缺陷的。见codepen.io/sathomas/pen/WmoaGw

标签: javascript css vue.js


【解决方案1】:

我怀疑 Vue 首先插入静态类并没有什么特别的原因;可能它只是镜像renderClass函数中输入参数的顺序。

CSS 文件中规则集的顺序也很重要;元素的class 属性中的类名顺序没有。并且这两个命令都与cascade 没有任何关系,cascade 指的是从父元素继承样式的子元素。也许您已经将其与块内或内联样式内的声明顺序混淆了。在这种情况下,顺序确实很重要:

<p class="red blue">
    Order doesn't matter in the class attribute above. If
    the class styles contradict, whichever is defined last
    will win regardless of how they're ordered in the attribute.
</p>

<p class="blue red">
    This paragraph will be styled identically to the previous
    one, despite the change in class order.
</p>

<p style="color: red; color: blue">
    Order does matter here. The text color will be blue.
</p>

【讨论】:

  • 谢谢!感谢您提供详尽而有帮助的解释!
  • 你到底是怎么知道在哪里引用这样的 renderClass 的?!除了 Vue 文档和论坛之外,是否有更详细地解释架构的阅读/文档?还是你只是在研究源文件?
  • 只是阅读源代码。尽管如果您正在寻找额外的信息,Vue Mastery 有一个关于 Vue 内部和架构的非常好的系列
猜你喜欢
  • 2020-02-04
  • 2012-11-08
  • 1970-01-01
  • 2020-12-02
  • 2010-11-22
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 2010-11-11
相关资源
最近更新 更多