When using Vue.js, the class binding feature may not function properly if it is referencing another data property that has variants

I am currently developing a basic TODO application. Within my index.html file, I have a main div with the id #app. Inside this div, there is another div with the class .todobox where I intend to display different tasks stored in my main.js file. Each task has a specific property called varStyle, which is meant to customize each .todobox accordingly. The purpose of this customization is to show completed tasks with a grey background and incomplete tasks with a red one.

Issue: Regardless of the type of varStyle I define, all tasks are displayed with the same styling as the main .todobox. Interestingly, there are no significant errors showing up in the console.

Any suggestions on how to resolve this problem would be greatly appreciated. Thank you in advance!

var todo = new Vue({
    el: '#app',
    data: {
        styleundone: {
            backgroundColor: 'crimson',
        },
        styledone: {
            textDecoration: 'line-through',
            backgroundColor: 'gray'
        },
        variants: [
            {
                varID: 2333,
                varDesc: 'Create a new instance',
                varStyle: this.styledone
            },
            {
                varID: 2345,
                varDesc: 'Boot up the computer',
                varStyle: this.styledone
            },
            {
                varID: 2787,
                varDesc: 'Open Google Chrome',
                varStyle: this.styledone
            }
        ],
    }
})
body {
    margin: 0
}

#app {
    margin: 2%;
    justify-content: center;
    align-items: center;
}

.header {
    height: 100px;
    background: linear-gradient(90deg, rgba(162,148,203,0.7651435574229692) 0%, rgba(228,147,205,1) 50%, rgba(169,163,214,0.7035189075630253) 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 30px;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
}

.todobox {
    display: inline-block;
    border: 2px black solid;
    border-radius: 5%;
    color: white;
    margin-left: 2rem;
    margin-top: 2rem;
    -webkit-box-shadow: 14px 10px 5px -1px rgba(255,153,255,1);
    -moz-box-shadow: 14px 10px 5px -1px rgba(255,153,255,1);
    box-shadow: 14px 10px 5px -1px rgba(255,153,255,1);
    -webkit-transition-duration: 0.4s;
    transition-duration: 0.4s;
}

.todobox:hover {
    cursor: pointer;
}

.todobox:active {
    box-shadow: none;
    transition: all 0.3s
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewpoint" content="width=devide-width, initial-scale=1">
        <link rel="stylesheet" href="style.css">
        <title>ToDo List</title>
    </head>
    <body>
        <div class="header">
            <h1>Todo List</h1>
        </div>
    
        <div id="app">
            
                <div class="todobox" 
                v-for="variant in variants"
                :key="variant.varID"
                :style="variant.varStyle">
                <p>{{ variant.varDesc }}</p>
                </div>

        </div>

        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script src="/main.js"></script>
    </body>
</html>

Answer №1

For accessing the data property styledone, define variants as a computed property instead of trying to refer directly within another data property:

Vue.config.devtools = false;
Vue.config.productionTip = false;


var todo = new Vue({
  el: '#app',
  data: {
    styleundone: {
      backgroundColor: 'crimson',
    },
    styledone: {
      textDecoration: 'line-through',
      backgroundColor: 'gray'
    },

  },
  computed: {
    variants() {

      return [{
          varID: 2333,
          varDesc: 'Create a new instance',
          varStyle: this.styledone
        },
        {
          varID: 2345,
          varDesc: 'Boot up the computer',
          varStyle: this.styledone
        },
        {
          varID: 2787,
          varDesc: 'Open Google Chrome',
          varStyle: this.styledone
        }
      ]

    }
  }
})
body {
  margin: 0
}

#app {
  margin: 2%;
  justify-content: center;
  align-items: center;
}

.header {
  height: 100px;
  background: linear-gradient(90deg, rgba(162, 148, 203, 0.7651435574229692) 0%, rgba(228, 147, 205, 1) 50%, rgba(169, 163, 214, 0.7035189075630253) 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 30px;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
}

.todobox {
  display: inline-block;
  border: 2px black solid;
  border-radius: 5%;
  color: white;
  margin-left: 2rem;
  margin-top: 2rem;
  -webkit-box-shadow: 14px 10px 5px -1px rgba(255, 153, 255, 1);
  -moz-box-shadow: 14px 10px 5px -1px rgba(255, 153, 255, 1);
  box-shadow: 14px 10px 5px -1px rgba(255, 153, 255, 1);
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
}

.todobox:hover {
  cursor: pointer;
}

.todobox:active {
  box-shadow: none;
  transition: all 0.3s
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<!DOCTYPE html>
<html>

<head>
  <meta name="viewpoint" content="width=devide-width, initial-scale=1"
  <link rel="stylesheet" href="style.css">
  <title>ToDo List</title>
</head>

<body>
  <div class="header">
    <h1>Todo List</h1>
  </div>

  <div id="app">

    <div class="todobox" v-for="variant in variants" :key="variant.varID" :style="variant.varStyle">
      <p>{{ variant.varDesc }}</p>
    </div>

  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="/main.js"></script>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on dynamically altering text upon hovering over an element using Vue.js

On my website, I have the following HTML code: <div class="greetings"> <img @mouseover="hover='A'" @mouseleave="hover=''" src="a.png" alt="A" /> <img @mouseover="hover='B'" @mouseleave="hover=''" ...

Error encountered: attempting to build for the iOS Simulator, although linking a dylib compilation specifically for iOS, such as the file 'TitaniumKit.framework/TitaniumKit' for the arm64 architecture

Whenever I attempt to build my appcelerator app for ios, I encounter this error: ld: building for iOS Simulator, but linking in dylib built for iOS, file 'Frameworks/TitaniumKit.framework/TitaniumKit' for architecture arm64 Despite my efforts to ...

The vanilla JS router seems to be malfunctioning, as it is only showing the [object XMLDocument]

Recently, I attempted to implement routing into my JavaScript application using the following example: link. However, after diligently copying every file from the project, all I see displayed inside the <main></main> tags is "[object XMLDocum ...

accessing the offsetTop property of React elements

When working in React, I've noticed that the offsetTop value of an element differs between componentDidMount and componentDidUpdate. This is surprising to me because I believed componentDidMount occurs after render, meaning the DOM elements should be ...

Having trouble with looping the CSS background using JavaScript in CodePen

Can someone help me with looping through CSS background images? Why is the background not changing in this code example: http://codepen.io/anon/pen/dGKYaJ const bg = $('.background').css('background-image'); let currentBackground = 0; ...

What is the best method to extract the values of objects in an array that share

var data= [{tharea: "Rare Disease", value: 3405220}, {tharea: "Rare Disease", value: 1108620}, {tharea: "Rare Disease", value: 9964980}, {tharea: "Rare Disease", value: 3881360}, ...

As the browser window is resized, the gap between images widens while the images themselves decrease

Hey there, I'm encountering some trouble with the image links at the top of my page. As I resize the browser or change screen resolutions in media queries using percentages, the images are resizing accordingly. However, I'm noticing that as the i ...

Is there a way for me to achieve a vertical page turning effect on a single page?

I am seeking to develop a unique calendar display consisting of 12 images that give the illusion of flipping up when clicked. While I am aware of turn.js, my knowledge of javascript is limited and I need guidance on how to proceed. Despite having a program ...

Unlocking the npm package event emitter on the client side in Meteor

Having some trouble with a seemingly basic issue. I came across an NPM package called LOG-WATCHER (used as an example) which keeps an eye on a specific log file on a client's local file system. This package emits events such as 'START', &apo ...

Modifying Bracket Shell Name leads to WebSocket Connection Failure

I have been working on developing an application using the Brackets shell. Specifically, I am in the process of creating a customized code editor for a project rather than starting from scratch by modifying Brackets. Up until now, I have managed to addres ...

Trouble with jQuery click event on dynamically generated elements

I have a jQuery function that iterates through each element inside a specified div (#container) and triggers a JavaScript alert whenever a span is clicked. Everything functions correctly when the spans are static. However, when I include code like this: ...

What is preventing me from accessing a function that is declared using function declaration while using a debugger?

When pausing at the debugger statement, an attempt to call foo results in a ReferenceError. It appears that the function is not defined within the script's context or scope, similar to how a local variable like x is. The script example.js is as follo ...

Contrast between pre-tag in HTML and white-space: pre newline exclusion

I originally thought that the pre(html tag) would act just like 'white-space:pre'. However, it turns out that it does not behave in the same way. <pre> aaa bbb </pre> <p style="white-space:pre;"> aaa bbb </p> The <pr ...

Utilizing JavaScript to Extract JSON Information from Knockout.js

Incorporating knockout into my project has been a great help as I retrieve JSON objects using Ajax. One question that arises is how to effectively utilize this data in my custom JavaScript code: After receiving the mapped item from the ajax call, here is ...

Storing map tiles offline in React-Leaflet

One common method I have come across for storing tiles offline with Leaflet involves using localforage. Here's an example: const map = L.map("map-id"); const offlineLayer = L.tileLayer.offline('https://server.arcgisonline.com/ArcGIS/res ...

Are there any cross-platform inter-process communication APIs available in both C++ and Javascript?

In the process of developing an app, I am faced with the challenge of passing messages between a C++ application and a Javascript web app. While I have experience writing sockets code in both languages when required, I am now looking for a higher-level me ...

Display all the names of the files from various file inputs in a unified notification

I have developed a function that checks if the selected file names already exist in the database. It currently alerts a message for each file name found in the database, which can be overwhelming with multiple files. I am looking for suggestions on how t ...

Vue.js app does not have the capability to display JSON data as a table

I successfully fetched JSON data in my vue app from the endpoint '/v1/api/sse?raceId=1', but I'm struggling to convert this JSON into a table format. The JSON is displaying correctly, but I'm unsure how to structure it into a table. He ...

Refreshing the page causes Firebase authentication to vanish

My React app integrates Firebase authentication. However, I am facing an issue where the firebase:authUser is stored in localStorage upon successful login, but gets cleared on every page refresh resulting in a lost session. Surprisingly, browsing through o ...

Tips for displaying a title within the border of a div

We have a client who is asking for a unique feature on their new website - they want the title to appear within the border of a text area. We need help figuring out how to achieve this using CSS/HTML. Take a look at the effect we are trying to create: htt ...