2

My component is like this :

<template>
    <div class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form id="form-message">
                    ...

                    <input type="text" class="form-control" id="subject" placeholder="Subject" v-model="product_name" required>
                    ...   
                </form>    
            </div>
        </div>
    </div>         
</template>

<script>
    export default {
        name: 'MessageModal',
        props: ['productName'],
        data() {
            return {
                product_name: this.productName,
            }
        }
    }
</script>

This component is a modal. This modal showed when click a button. When the modal component showed, I want send value of product_name property to value of input text

I try like above code, but the input text not display value of product_name property

If I put input text out the form like this :

<input type="text" class="form-control" id="subject" placeholder="Subject" v-model="product_name" required>
<form id="form-message">
    ...
</form>    

It works. But why If I put input text in the form, it does not work?

How can I solve it?

9
  • @Bert Evans, See my question. I had update it Commented Mar 8, 2017 at 19:56
  • @Bert Evans, If I put input text out the form, it works. But why If I put input text in the form, it does not work? Commented Mar 8, 2017 at 20:28
  • Good question :) Commented Mar 8, 2017 at 20:29
  • It's possible there's some other library on the page that clears form inputs in certain cases? I don't think it's a Vue issue. Commented Mar 8, 2017 at 20:37
  • @Bert Evans, Look at this : jsfiddle.net/oscar11/d3jecL8n/5. It does not work Commented Mar 8, 2017 at 21:03

1 Answer 1

1

I would do it like this:

<template>
...
    <input type="text" class="form-control" id="subject" placeholder="Subject" v-model="pname" required>
...                
</template>

<script>
  export default {
    mounted() {
      this.pname = this.productName
    },
    props: ['productName'],
    data() {
        return {
            pname: "",
        }
    }
  }
</script>

This way when component is mounted you set prop to internal data property pname of component and bind value of input field with pname.

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

1 Comment

It does not work. You try see my question. I had update it

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.