Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS v-bind not working with kebab-case

I am working on a task list with Vue.JS (like everyone) and I've managed to add a task, show the tasks and even to delete the tasks. After that I was working on checking tasks and give them a class for success.

So I thought, if I have a data like class set to false, and use this line of code:

<div v-for="(task, index) in tasks" class="panel panel-default" :class="{panel-success: task.class}" :key="task">

I could set the class to true with a click event and give the particular class of panel-success (from bootstrap).

When I was doing that, I came up to the following problem:

avoid using JavaScript keyword as property name: "class" in expression :class="{panel-success: task.class}"

The problem was, the kebab case syntax of panel-success. When I changed the name to panelsuccess it was working. Why is kebab case not working?

like image 200
Giesburts Avatar asked Dec 17 '25 21:12

Giesburts


1 Answers

The value for :class is a Javascript object, and in Javascript objects a kebab-case identifier is not valid, that's why you're having that error. For it to work, simply wrap your kebab-case identifier around single quotes:

:class="{'panel-success': task.class}"
like image 114
Arnelle Balane Avatar answered Dec 20 '25 11:12

Arnelle Balane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!