JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Setting up checkbox functionality in Vue

Author:JIYIK Last Updated:2025/03/01 Views:

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.


Using the v-model directive

The directive in Vue v-modelcan implement two-way data binding, thereby synchronizing the checkbox selection state with the data model. For example, we can define a checkbox component like this:

<template>
  <div>
    <label>
      <input type="checkbox" v-model="checked" />
      {{ label }}
    </label>
  </div>
</template>

<script>
export default {
  props: {
    label: String,
    value: Boolean,
  },
  data() {
    return {
      checked: this.value,
    };
  },
  watch: {
    checked(newVal) {
      this.$emit("input", newVal);
    },
  },
};
</script>

In this component, we use v-modelthe directive to bind the checked property and initialize the value of checked in data . When the user clicks the checkbox, the value of checked is automatically updated, triggering watchthe callback function in and passing the latest value $emitto the parent component through the method.

When using this checkbox component, we can write it like this:

<template>
  <div>
    <checkbox v-model="checked" label="选项1" />
    <checkbox v-model="checked" label="选项2" />
    <checkbox v-model="checked" label="选项3" />
  </div>
</template>

<script>
import Checkbox from "./Checkbox.vue";

export default {
  components: {
    Checkbox,
  },
  data() {
    return {
      checked: [false, false, false],
    };
  },
};
</script>

In this parent component, we use three checkbox components and v-modelbind them to the same array checked . Each element in this array corresponds to a checkbox. When the user selects a checkbox, the corresponding element in the checked array is automatically updated.


Use v-bind to bind the checked attribute

In addition to using v-modelthe directive, we can also use v-bindthe directive to bind the checked attribute to synchronize the checkbox's selection state with the data model. For example, we can define a checkbox component like this:

<template>
  <div>
    <label>
      <input type="checkbox" v-bind:checked="checked" v-on:change="handleChange" />
      {{ label }}
    </label>
  </div>
</template>

<script>
export default {
  props: {
    label: String,
    value: Boolean,
  },
  data() {
    return {
      checked: this.value,
    };
  },
  methods: {
    handleChange(event) {
      this.checked = event.target.checked;
      this.$emit("input", this.checked);
    },
  },
};
</script>

In this component, we use v-bindthe directive to bind the checked property and initialize the checked value in data . When the user clicks the checkbox, the change event is triggered, which calls the method and passes the latest value to the parent component through the method.handleChange$emit

When using this checkbox component, we can write it like this:

<template>
  <div>
    <checkbox v-bind:checked="checked[0]" v-on:input="checked[0] = $event.target.checked" label="选项1" />
    <checkbox v-bind:checked="checked[1]" v-on:input="checked[1] = $event.target.checked" label="选项2" />
    <checkbox v-bind:checked="checked[2]" v-on:input="checked[2] = $event.target.checked" label="选项3" />
  </div>
</template>

<script>
import Checkbox from "./Checkbox.vue";

export default {
  components: {
    Checkbox,
  },
  data() {
    return {
      checked: [false, false, false],
    };
  },
};
</script>

In this parent component, we use three checkbox components and bind their checked properties to the corresponding elements of the checked array. When the user selects a checkbox, the corresponding checked array element is automatically updated.


Summarize

This article introduces two methods of setting the checkbox function in Vuev-model : using the directive and using v-bindthe directive. Regardless of which method is used, it is necessary to synchronize the selection state of the checkbox with the data model to ensure the correctness of the data. In actual development, we can choose different methods according to specific needs and demonstrate them with actual examples.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

What is the use of the immediate property of watch in Vue?

Publish Date:2025/03/01 Views:65 Category:Vue

In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang

What happens if a child component changes the data in props in Vue?

Publish Date:2025/03/01 Views:138 Category:Vue

In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t

How to refresh the page in Vue

Publish Date:2025/03/01 Views:85 Category:Vue

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

How to find all elements by class name in Vue

Publish Date:2025/03/01 Views:182 Category:Vue

Vue is a very powerful JavaScript framework that provides developers with a lot of convenient features and tools. One of them is finding all elements by class name. In this article, we will explore how to find all elements by class name in

How to get the initial state of a certain data in Vue?

Publish Date:2025/03/01 Views:146 Category:Vue

In Vue, sometimes we want to get the initial state of a data in data so that we can compare or reset it in subsequent operations. In this article, we will introduce how to get the initial state of a data in data and provide an example to il

Implementation of scheduling execution in Vue project

Publish Date:2025/03/01 Views:128 Category:Vue

In Vue projects, scheduling execution refers to assigning tasks to different components or instances to implement functions such as data processing, rendering, and interaction. This article will introduce the implementation method of schedul

Defining global components in Vue

Publish Date:2025/03/01 Views:99 Category:Vue

In Vue, global components are a very important concept that allows us to use the same components everywhere. In this article, we will discuss how to define global components in Vue and the relevant precautions.

How to use cookies or localstorage for data persistence in Vue

Publish Date:2025/03/01 Views:84 Category:Vue

In Vue, we usually need to persist data so that the data state can be retained after the user refreshes the page or closes the browser. Common implementation methods include using cookies and localstorage. Using cookies Cookies are a simple

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial