Matheus Hernandes
Matheus Hernandes

Reputation: 659

Return model value formatted with Vue.js

I have a table in Vue.js application, listing a URL and an Id. This URL is defined by the user, so I created and input component, with a input text, using the URL as value and the Id as parameter. The v-model of this component is an array, where I need to store data as JSON objects like this:

{ 
    "id": 1, 
    "url": "www.some-url.com" 
}

How can I catch changes in the url field and return for its parent to append in an array?

Component:

<template>
  <div class="row">
      <div class="col-md-12">
        <input type="text"
               class="form-control"
               v-model="value.url">
      </div>
  </div>
</template>
<script>
  export default {
    name: 'inputUrl',
    props: {
      url: {
        type: [String],
        description: 'URL'
      },
      id: {
        type: Number,
        description: 'Id'
      }
    },
    components: {
    }
    data() {
      return {
        value: {
          id: this.id,
          url: this.default
        }
      };
    },
    methods: {
    },
    mounted() {
    },
    watch: {
    }
  }
</script>

Usage:

<inputUrl
    :id="1"
    url="www.some-url.com"
    v-model="array">
</inputUrl>

Upvotes: 0

Views: 565

Answers (1)

Abdelrhman Arnos
Abdelrhman Arnos

Reputation: 1442

I passed the array variable to the InputUrl component then used v-on directive to passing the current input value to a custom function for appending the new values to the array variable. Here an example.

Upvotes: 2

Related Questions