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

Is it possible to execute an npm package without using the npm run command?

Is there a way to initiate the next.js build process directly through the command line without needing to use the package.json file? Can we execute it without relying on npm run? Perhaps running next build in the command line could achieve this instead of ...

Angular 13 does not currently have support for the experimental syntax 'importMeta' activated

Since upgrading to angular 13, I've encountered an issue while attempting to create a worker in the following manner: new Worker(new URL('../path/to/worker', import.meta.url), {type: 'module'}) This code works as expected with "ng ...

There seems to be an issue with AJAX form submission and it is not functioning properly

Having trouble submitting a form to another page using ajax, as it is not sending the post request. I have included Javascript at the top of the page: <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script> $(function(){ ...

Encountering NodeJs Error 401(Unauthorized) while implementing passport-jwt in my project

I am currently developing an authentication application using Node.js, MongoDB, and the Passport-JWT middleware. I have successfully implemented the login functionality and I am able to obtain a token. However, when trying to access the user profile after ...

How to efficiently assign a random set of values to a group of players in Javascript/nodejs while ensuring each player does not receive their own inputted value

Within my array, I have stored the following information: players[{nickname: data.player, id: socket.id, data: playerdata}] The playerdata itself is an array playerdata[] In the first value of playerdata, each player has entered a string. (playerindex ...

Having difficulty accessing the Material UI Icons

I encountered an issue when attempting to utilize Material UI icons - as soon as I added the icon component, an error occurred. https://i.stack.imgur.com/issmm.png For reference, you can find the code on CodeSandbox at the following link: https://codesand ...

Utilize Google Maps to receive directions to a specific destination and discover current traffic conditions along the route using their comprehensive API

I am currently working on implementing the Google Maps direction identifier functionality with traffic details. However, I am able to obtain directions but not specific traffic details for a selected path; it seems to apply overall traffic data instead. ...

Is it possible to interpret all events from multiple perspectives?

Is it possible to listen for events in three different ways? This example shows how we can listen for the load event: 1. <body onload="doSomething();"> 2. document.body.onload = doSomething; 3. document.body.addEventListener('load', doS ...

What could be the reason for v-model not functioning properly?

Embarking on my Vue.js coding journey, I am currently following a straightforward online tutorial. Utilizing the vue-cli, I kickstarted my application and crafted a basic component named Test.vue. Within this component lies a simplistic input control conne ...

Error when compiling TypeScript: The callback function provided in Array.map is not callable

This is a Node.js API that has been written in Typescript. app.post('/photos/upload', upload.array('photos', 12), async (req, res) => { var response = { } var list = [] try { const col = await loadCollection(COLLECTION_NAM ...

Setting the width of colspan elements can be challenging when other columns have dynamic widths

I am working with a table setup that includes various columns for data tracking: <table > <thead style="width: calc(100% - 15px); display: table; table-layout: fixed;"> <tr> <th colspan="3">& ...

The prototype chaining for JavaScript inheritance was not functioning as expected

I have been studying Stoyan Stefanov's book on JavaScript Patterns and I seem to be stuck...I am having trouble inheriting the prototype. What could I possibly be doing wrong? (Please execute this code in NodeJS) // implementing inheritance function ...

Endless loop issue in Reactjs encountered when utilizing React Hooks

Delving into React Hooks as a newcomer, I encountered an error that has me stumped. The console points to an infinite loop in the code but I can't pinpoint the exact line responsible. Too many re-renders. React limits the number of renders to prevent ...

Save the JSON data into a variable inside a React utility component

Currently, I am new to React and facing an issue with fetching data and storing it in a variable. I have been struggling to understand why my SetMovieResponse function is not working as expected. I have tried stringifying the JSON before sending it, but w ...

Is there a way to retrieve the ID by using the autocomplete feature to search for a user's first name and last name in PHP

Check out this code from index.php (all credit to the original owner): <HTML> <HEAD> <TITLE> Ajax php Auto Suggest </TITLE> <link href="css/style.css" rel="stylesheet" type="text/css"> <SCRIPT LANGUAGE="JavaScript ...

Nesting CSS classes allows for more specific targeting of

I am a bit rusty on CSS, it's been about 5-7 years since I last worked with it. Could someone help me come up with a solution to my problem? Here's the ideal design I have in mind: table.ctable { class:collapsible collapsed; } I know this syn ...

Exploring the contrast between Vuex store WATCH and SUBSCRIBE

Can you explain the main distinction between watch and subscribe, and when it is most appropriate to use one over the other? According to information on the Vuex official documentation, both methods appear to be essentially identical in functionality and p ...

Nuxt Static Site encountering issues with displaying dynamic content on 404 Error page

Edit I recently resolved the issue of the error template not loading correctly by fixing an htaccess problem. By adjusting the ErrorDocument to be /sub/dirs/error.html (without the dist folder), I was able to get it to work properly. However, I am still fa ...

Is there a native feature in Vue.js that allows for the addition of a duplicate of a constantly saved object to an array that is repeated

Having an issue with my Vue.js app where adding a newItem to a list of items results in the added object being bound to the input. Here's what I've tried so far: new Vue({ el: '#demo', data: { items: [ { start: & ...

Can you explain the purpose and function of stub.callsArg(index) feature in Sinon.JS?

Confusion has set in as I try to make sense of this. According to the documentation: stub.callsArg(index) - This command prompts the stub to execute the callback function found at the specified index. For instance, using stub.callsArg(0); will trigger the ...