let’s we have a data variable and i want to use this data variable for my style.
data()
{
return{
selected:{
index: 2
}
}
}
<style>
.parent-table >>> .table-row:nth-child(/* here i want to pass selected.index */) {
background: red;
}
</style>
My use case is that i have a table component used in my landing page . I want to change background of selected row of table from my landing page.
3
Answers
I don’t think we have any solution as per your requirement in
Vue 2
. InVue 3.2
, They introduced this feature which enables component state-driven dynamic CSS values in<style>
tags. You can read that outhere
.After understand your exact requirement and post spending few hours on this requirement, Here I am coming up with the solution in JavaScript way as we can’t use dynamic variables in CSS
nth-child
selectors. You can have a look in this SO post :Is it possible to use CSS vars in CSS3 selectors?
Looks like you have to do update the style of
nth-child
in pure JS way.Working Demo :
I don’t know how to explain it. but here’s how to pass
variables
tostyle scope
PROPS
:COMPUTED
(variables that can be used as arguments):Example of accessing the
variables
insidestyle scoped
:note: You don’t need to make the props if you only want to get the
index
from yourdata