Skip to content
Advertisement

How bind data in vue.js when my key consists dash (-) in JSON data?

My JSON data looks like:

JavaScript

How can I bind this variable with style tag for vue component?
I am trying to use it in the following way, but its not working.

JavaScript

Advertisement

Answer

You would generally access your component’s data properties in the template by directly referring to them, without prefixing this. or data.. In your case, that’s a problem, because it prevents you from using bracket notation.

You have two solutions:

Solution 1: Don’t put your JSON data directly in the component data, wrap it in an object (e.g. colors). This way, using colors['color-1'] in the template will work.

Solution 2: Leave your data as is and write a simple method for getting properties in your component, then call it from the template. Something like:

JavaScript

And then in the template:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement