0

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>
1
  • 1
    You can not use v-model with custom components only if you add v-bind:value="value" v-on:input="$emit('input', $event.target.value)" in your input inside your component to capture changes. Commented Apr 28, 2020 at 0:15

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.