简体   繁体   English

了解Vue.js CSS类绑定顺序

[英]Understanding Vue.js CSS Class Binding Ordering

Can anyone help me understand how to control the ordering of a component root element css class and any css class that may be bound from the parent calling the component? 谁能帮助我了解如何控制组件根元素css类以及可能与调用该组件的父对象绑定的任何css类的顺序?

Here is a fiddle that depicts what I'm noticing (snippet example below): https://jsfiddle.net/cicsolutions/b6rnaw25/ 这是描述我所注意的小提琴(下面的片段示例): https : //jsfiddle.net/cicsolutions/b6rnaw25/

You'll notice if you have a component with a class on its root element, if that class is a string, Vue's class binding places the class at the beginning of the resulting bound class list. 您会注意到,如果您的组件的根元素上有一个类,如果该类是字符串,则Vue的类绑定会将类放在结果绑定的类列表的开头。 This is what I would expect because because the component sets the base css class and then you can customize the styles when you use the component by adding classes to the component html element. 这是我所期望的,因为组件设置了基本的CSS类,然后在使用组件时可以通过将类添加到组件html元素中来自定义样式。 Then Vue binds/joins the classes together. 然后,Vue将这些类绑定/合并在一起。

In the next examples in the fiddle, I'm showing the use of a css class that is dynamic (ie not a static string). 在小提琴中的下一个示例中,我展示了动态css类的使用(即,不是静态字符串)。 In these cases, Vue places the component's root element class at the end of the bound class list. 在这些情况下,Vue将组件的根元素类放在绑定的类列表的末尾。

I'm working on a component that I hope others will use, so I'd like to set my component class on the root element, and then if anyone wants to override those styles, they can just add their own class on the component tag. 我正在开发一个希望其他人使用的组件,所以我想将我的组件类设置在root元素上,然后,如果有人想覆盖这些样式,他们可以在自己的组件标签上添加自己的类。

I also need the root element class to be dynamic, so I must use an array or an object to handle the class binding. 我还需要根元素类是动态的,因此我必须使用数组或对象来处理类绑定。

Does anyone know why Vue places the component root css class at the beginning for static classes and at the end for dynamic classes? 有谁知道为什么Vue在静态类的开头和动态类的末尾放置组件根css类? That seems strange to me, but perhaps it's intentional for a reason that eludes me. 这对我来说似乎很奇怪,但是也许出于某种原因是故意的。

None the less, how would I go about ensuring that my component's root element class is always first in the resulting bound class list, when I need it to be a dynamic class? 但是,当我需要将其作为动态类时,如何确保组件的根元素类始终在结果绑定类列表中排在首位?

 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> 

I suspect that there's no particular reason why Vue inserts static classes first; 我怀疑Vue首先插入静态类没有特别的原因。 possibly it's just mirroring the order of the input parameters in the renderClass function. 可能只是镜像renderClass函数中输入参数的renderClass

Also the order of rule sets in CSS files matters; CSS文件中规则集的顺序也很重要。 the order of class names in the class attribute of elements does not. 类名称在元素的class属性中的顺序不正确。 And neither order has anything to do with the cascade , which refers to child elements inheriting styles from their parents. 而且这两个顺序都与级联无关, 级联是指子元素从其父级继承样式。 Perhaps you've confused that with the order of declarations within a block or within an inline style. 也许您已经将其与块内或内联样式中的声明顺序混淆了。 In that case order does matter: 在这种情况下,顺序很重要:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM