Connect Vue-switch value to a data property

Utilizing the vue-switch component to toggle on and off, I needed to determine its current state. Following guidance from the documentation at https://www.npmjs.com/package/vue-switch, I implemented :value.sync="toggle" in the switch component along with a data property called 'toggle'. However, an error message has surfaced:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value" vue switch found in ---> <Switcher> at E:\Dev\BackgroundCheck.Members\front-end\node_modules\vue-switch\switch.vue

The HTML snippet:

<switcher size="lg" color="green" :value.sync="toggle"></switcher>

JS implementation:

<script>
import mySwitch from 'vue-switch';

export default {
  name: 'BackgroundReports',
  components: {
    'switcher': mySwitch
  },
  computed: {
    report() { return this.$store.getters.currentReport },
    isBeingMonitored() { return this.$store.getters.isBeingMonitored }
  },
  mounted() {
    this.toggle = this.isBeingMonitored;
  }
}
</script>

Content of switch.vue file:

<template>
    <div :class="className" @click="onClick">
        <span class="open">{{ openName }}</span>
        <span class="close">{{ closeName }}</span>
    </div>
</template>

<script>
'use strict';

export default {
    props: {
        value: {
            default: true,
            twoWay: true
        },
        size: {
            type: String,
            default: 'md中'
        },
        // blue red green orange
        color: {
            type: String,
            default: 'red'
        },
        openValue: {
            default: true
        },
        closeValue: {
            default: false
        },
        openName: {
            type: String,
            default: 'ON'
        },
        closeName: {
            type: String,
            default: 'OFF'
        },
        disabled: {
            type: Boolean,
            default: false
        }
    },
    computed: {
        className() {
            let {
                value,
                openValue,
                closeValue,
                size,
                color,
                disabled
            } = this;
            return {
                'vue-switch': true,
                'z-on': value === openValue,
                'z-disabled': disabled,
                ['s-' + size]: true,
                ['c-' + color]: true
            };
        }
    },
    methods: {
        onClick() {
            let {
                disabled,
                value,
                openValue,
                closeValue
            } = this;
            if (!disabled) {
                if (openValue === value) {
                    this.value = closeValue;
                } else {
                    this.value = openValue;
                }
            }
        }
    }
}

</script>

Answer №1

Upon closer inspection, it becomes clear that the package includes two distinct components. One is designed for vue 1.* while the other is tailored for vue 2.*. Make sure you import the appropriate one according to your needs.

import mySwitch from 'vue-switch/switch-2.vue';

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

Swapping Out Models in Three.js: A Guide to Replacing Object3D Models

I'm attempting to swap out an object3D for a character model, while retaining all of its attributes like the position. var object = new THREE.Object3D( ); object = models.character[0].clone( ); object.position.set( 100, 230, 15 ); scene.add( object.sc ...

Implement Meteor authentication with Google OAuth

I'm attempting to set up a basic login button for my Meteor app using google oauth. I followed these steps: mrt create accounts mrt add accounts-google mrt add accounts-ui I removed the default html/css/js files and instead added: client/index.html ...

Steps to creating a custom text editor using React for generating blog content and storing it in a MongoDB database

I have a challenge of building a rich text editor for my web app, specifically for creating blog posts that will be saved in the database for user viewing. Initially, I planned to use a form with input fields where the title and content of the blog post w ...

What is the best method for directing a search URL with an embedded query string?

Currently, I am developing an express application that has two different GET URLs. The first URL retrieves all resources from the database but is protected by authentication and requires admin access. The second URL fetches resources based on a search para ...

Creating an array from a numerical value in Angular Controls

In my application, I need to create an array starting from 1 up to a specified number, which in this case is 6, using JavaScript/TypeScript. Here is my attempted code: this.builder.group({ 'staff': this.builder.group({ staf ...

What is the best way to reference a nested jQuery variable, whether global or local, within an HTML document

I've been working on this problem for days with no success. Any help would be greatly appreciated. I'm trying to call a variable from nested jQuery into HTML. The variable is named "PROBLEM". Even after trying to make it global, any updates made ...

unleashing the magic of AJAX: a guide to extracting

In my Symfony project, I am attempting to retrieve the content of an AJAX request in order to check the data using dump(). The purpose is to process this data and perform a SQL query. However, when I use dump() in my controller, there doesn't appear t ...

The <v-select> component in VueJS fails to bind properly during a post operation

Currently, I am in the process of developing an asp.net mvc application that utilizes VueJS on the front end. While working with a variety of forms, I have encountered an issue where text fields function properly when submitting the form; however, this is ...

Comparison of Grant and Passport.js: Which One to Choose?

Are you unsure about the distinctions between Grant and Passport.js? How do you decide when to utilize Grant over passport.js, and vice versa? If your goal is to create a social media platform that tracks user activities and posts them on a news feed, whi ...

What is the process for animating classes instead of ids in CSS?

My webpage currently features a setup similar to this. function wiggle(){ var parent = document.getElementById("wiggle"); var string = parent.innerHTML; parent.innerHTML = ""; string.split(""); var i = 0, length = string.length; for (i; i ...

I need to search through a tree structure in typescript based on a specific value without encountering a maximum stack call exceeded error

How can I perform a value-based search on a complex tree structure in TypeScript without encountering the maximum stack call exceeded error? I am attempting to navigate through an expandable tree using TypeScript, and I will provide the code snippet below ...

Using Ajax to send a JSON object containing two arrays to a servlet, then parsing it in a Java servlet without the use

When sending data to a servlet, I utilize an Ajax POST call to send a JSON object containing two arrays. In the servlet class in Java, I need to read this data sent in two lists or arrays. I am avoiding the use of jQuery for the Ajax call and am not very f ...

Utilize the Algolia search-insights library in conjunction with React

Trying to integrate the Search-Insights Library from Algolia with React using npm for installation instead of just adding it to the header as shown in the example. Example of implementation in React Click here for React implementation example <script ...

Tips on generating an HTML element using JavaScript and storing it in a MySQL database

I need help with saving a created element to the database so that it remains on the page even after refreshing. Any assistance would be greatly appreciated. Thank you. document.getElementById("insert").onclick = function(){ if(document.getElementById( ...

Retrieving Data with AngularJS: Making HTTP Calls and Handling Responses

I seem to be facing a bit of a hurdle in executing an HTTP GET request and retrieving the data properly in JavaScript. The goal is to attach this data to the global window variable, but I'm encountering some difficulty. Here is the code snippet for t ...

Guide to running a NextJS app alongside an Express server backend on the same localhost port

I'm working on hosting my NextJS app, which is built with React, on the same localhost port as my express api-server backend. Within my express server API settings, I have configured my API server to listen on: http://localhost:3000/graphql How can ...

Exploring the world of JSON and JavaScript data structures

Can someone provide some clarification please? var a = '{"item":"earth", "color":"blue", "weight":920}'; Is the data type of a a string or an array ? var b = JSON.parse(a); What is the data type of b - an object or an array ? ...

How to Override Certificate Expiry in JavaScript

Is there a way to bypass security for an expired certificate when making an Https post request in Javascript? I have been successful in doing this with C# and Java, but unsure about how to proceed in JavaScript. function post() { var xmlhttp; xmlhtt ...

Uncertainty surrounding the concept of JavaScript objects

What is the reason for this not functioning properly? function displayValue(a,b){ this.a = a; this.b = b; $('p').text(a + " " + b); } var display = new displayValue(); display('1','2'); How does this still work ...

When utilizing getServerSideProps, the data is provided without triggering a re-render

Although my approach may not align with SSR, my goal is to render all products when a user visits /products. This works perfectly using a simple products.map(...). I also have a category filter set up where clicking on a checkbox routes to /products?catego ...