When utilizing a computed property that accesses the Vuex state, the v-if directive alone may not function as expected without

Uncertain of the source of the issue, but the basic v-if functionality seems to be malfunctioning.

<template>
   <div>
       <select v-model="col.code">
            <option v-for="i in foo" :value="i.code">{{ i.code }}</option>
       </select>

       {{ col }}
       // { "code": "B" }

       <div v-if="col.code"&quuot;> // not working
            {{ col.code }}
       </div>
   </div>
</template>


<script>
export default {
    data() {
       return {
           foo: [
            {code: 'A'},
            {code: 'B'},
            {code: 'C'}
           ]
       } 
    },
    created() {
        this.view();
    },
    watch: {
        '$route': 'view',
    },
    computed: {
        col() {
            return this.$store.state.col;
        }
    }
}
</script>

However, when I introduce v-else, the desired outcome is achieved.

I've also observed that without using a computed property and directly utilizing a data property, the behavior remains inconsistent. Wrapping the v-if statements with <div> tags appears to resolve the issue.

<div>

    <select v-model="col.code">
        <option v-for="i in [1,2,3,4,5]" :value="i">{{ i }}</option>
    </select>

    <div v-if="col.code">
        {{ col }}
    </div>

</div>

Odd situation indeed.

Answer №1

It seems like there is an issue with

<div v-if="col.code">
, but the problem lies in binding the select to a computed property. You should instead use a computed setter :

computed: {
        col: {
            get() {
              return this.$store.state.col;
             
              },
            set(newVal) {
              this.$store.commit('UPDATE_COL', newVal); 
            }  
        }
    }

Vue.use(Vuex)
//store instance
const store = new Vuex.Store({
  state: {
    col: {
      code: 'A'
    }
  },
  mutations: {
    UPDATE_COL(state, payload) {
      state.col = payload
    }
  }
})

//vue instance
let app = new Vue({
  el: '#app',
  store,
  data() {
    return {
      foo: [{
          code: 'A'
        },
        {
          code: 'B'
        },
        {
          code: 'C'
        }
      ]
    }
  },
  created() {

  },

  computed: {
    col: {
      get() {
        return this.$store.state.col;

      },
      set(newVal) {
        this.$store.commit('UPDATE_COL', newVal);
      }
    }
  }

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vuex"></script>

<div id="app">
  <select v-model="col.code">
    <option v-for="i in foo" :value="i.code">{{ i.code }}</option>
  </select>


  <div v-if="col.code">
   My code : {{ col.code }}
  </div>

</div>

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

The error message in Express points to module.js line 550 and states that the module cannot be

I am currently in the process of setting up a basic express application using the code below: const express = require('express'); const app = express() const bodyParser = require('body-parser'); const cookieParser = require('cooki ...

Refresh your webpage with new content and modified URLs without the need to reload using window.history.pushState

Hey everyone, I'm looking to incorporate window.history.pushState into my website, but I'm unsure of how to go about it... What I'm aiming for is to have a page dashboard.php and update the URL to dashboard.php?do=edit&who=me, while loa ...

Load texture using ImageUtils with callback in Canvas Renderer

Currently, I am utilizing three.js revision 53. While attempting to load a texture in Canvas Renderer (specifically on Win7) and incorporating a callback for the onLoad event, the texture fails to display. Surprisingly enough, removing the callback functi ...

Sharing data between VueJS2 components

I am working on a project where the parent component retrieves a large JSON object from the server. My goal is to parse this data and then pass it down to the child components. The structure of the child components is as follows: <child-comp /> I ...

The Date.UTC function is not providing the correct output

While attempting to change Unix timestamps into a more understandable format, I used Date.UTC(2017,09,23);. When running this code, it returned 1508716800000. However, upon verifying on the website , the displayed result showed October 23, 2017 instead o ...

Clicking on a single checkbox causes the entire input to become deactivated due to the way the system is

I'm encountering a puzzling issue that has me feeling like I know the solution, yet I don't. I set "State" to [checked]. The problem arises when, upon turning it into a map and clicking just one checkbox, the entire selection is clicked. To addre ...

What is the best way to generate a new object within a function and then return it

The function performs the following steps: Retrieves an XML document through AJAX. Identifies relevant information within the document. Dynamically converts it into an object using a for loop. How can I access this generated object outside of the functi ...

Issue with watching functionality in Vue-cli 3 on Ubuntu operating system while using VirtualBox with Win10

I recently cloned a project from Github and deployed it on a virtual machine. Everything is functioning properly except for the ability to watch for updates in any files within the project. In the VagrantFile, there is a line of code to sync a folder: con ...

Error: The method 'send' is not available for the object #<ServerResponse>

So I've been diving into building this Express app and all of a sudden, I run into this strange error message that reads TypeError: Object #<ServerResponse> has no method 'send'. It popped up when I was setting up some routing using th ...

Please indicate the maximum number of digits allowed after the decimal point in the input

My input form uses a comma as the decimal separator: HTML: <form id="myform"> <label for="field">Required, decimal number:</label> <input class="left" id="field" name="field"> <br/> <input type="submit" va ...

What is the best way to eliminate the space underneath a graph?

Despite trying multiple solutions, I'm still struggling to remove the excess blue space below the graph that appears when clicking the submit button. Any insights into what might be causing this issue? JSFIDDLE Below are the CSS styles related to t ...

Styling of global checkbox focus in Material UI (applied globally, not locally)

Currently experimenting with customizing the styling of a checkbox when it is in focus globally using Material-UI (react). At present, only default and hover styles are taking effect: MuiCheckbox: { colorSecondary: { color: 'green', & ...

A guide on retrieving information from a database with PHP and transferring it to Javascript

As I work on creating a website using HTML, CSS, MySQL, and JavaScript, my goal is to allow users to log in and engage in a quiz with 40 questions. Below is the JavaScript code for a countdown timer that includes the "questions" variable. After 40 seconds ...

The term 'EmployeeContext' is being utilized as a namespace in this scenario, although it actually pertains to a type.ts(2702)

<EmployeeContext.Provider> value={addEmployee, DefaultData, sortedEmployees, deleteEmployee, updateEmployee} {props.children}; </EmployeeContext.Provider> I am currently facing an issue mentioned in the title. Could anyone lend a hand? ...

Find the position of an HTML5 canvas element within a webpage

I am currently facing an issue with a canvas within another canvas. <canvas id ='canvas2' height="718" width="1316"></canvas> The CSS for this canvas is as follows: #canvas2{ position:absolute; width :95%; height:90%; top:5%; lef ...

What is the best way to create a grid item that maximizes the available space in ReactJS Material-UI?

Currently, I am working with the material-ui grid system in a reactjs project. In a column grid container layout, my goal is to display two grid items and have a third item fill up the remaining space between them. Essentially, I want it to look like this: ...

How can the "not selected" option be disabled in a Vue Select component?

I have implemented a select box in the following manner: JS: Vue.component("v-select", VueSelect.VueSelect); new Vue({ el: "#app", data: { options: [ { countryCode: "AU", countryName: "Australia" }, { countryCode: "CA", countryName: " ...

Creating dynamic ng-options in AngularJS

Below is an array: $scope.age = 2; $scope.people = [{name:"Sam",age:2},{name:"Pam",age:3},{name:"Ham",age:4}] The requirement is to make the ng-options dynamic. When age is 2, display all people objects in ng-options. If age is 1, show only the object wi ...

Combining the power of jQuery, PHP, JavaScript, and the popular WordPress platform, let's unlock

After going through numerous attempts to find answers for similar issues, I'm unable to get any of the suggested solutions to work. My WordPress site requires a plugin that utilizes jQuery. The main file for my plugin is located at wp-content/plugins ...

The attribute of the Angular div tag that lacks an equal sign

Apologies if this question has been asked before. I've noticed in some people's code that they use the following syntax: <div ui-grid="myUIGrid" ui-grid-selection ui-grid-resize-columns class="grid" /> Can someone explain what ui-grid-sel ...