Home > Software design >  Two way form data binding in vue 3 composition api
Two way form data binding in vue 3 composition api

Time:03-21

I'm starting my transition from options api to compositon api in vue.

And I have the following block of code where I'm trying to implement 2 way bind.

I'm using ant-design-vue library for slider input. And trying to bind the slider value to input field.

<template>
  <div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model="inputValue1" :min="1" :max="20" />
      </a-col>
      <a-col :span="4">
        <a-input-number
          v-model="inputValue1"
          :min="1"
          :max="20"
          style="marginleft: 16px"
        />
      </a-col>
    </a-row>
  </div>
</template>
<script>
import { ref } from "vue";
export default {
  setup() {
    let inputValue1 = ref(8);
    return { inputValue1 };
  },
};
</script>

With the above code the value of inputValue1 doesn't change while inspecting in vue dev tool.

How do I work with two way binding in vue 3 composition api ?

Link to sandbox: https://stackblitz.com/edit/vue-pchhub?file=src/App.vue

CodePudding user response:

Looks like you have to use v-model:value="..."for this to work.

<template>
  <div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model:value="inputValue1" :min="1" :max="20" />
      </a-col>
      <a-col :span="4">
        <a-input-number
          v-model:value="inputValue1"
          :min="1"
          :max="20"
          style="marginleft: 16px"
        />
      </a-col>
    </a-row>
  </div>
</template>
<script>
import { ref } from "vue";
export default {
  setup() {
    let inputValue1 = ref(8);
    return { inputValue1 };
  },
};
</script>
  • Related