Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue-Laravel form Submission

I have a form created in vue and added to a blade page.

app.js

new Vue({
    router,
    components: {
        'advice-form': AdviceForm,
    }
}).$mount('#app');

blade.php

<div class="my-6">
    <advice-form></advice-form>
</div>

AdviceForm Component

<template>
  <input
    class="md:w-auto w-full"
    type="text"
    name="name"
    id="name"
    placeholder="Full name"
    v-model="name"
/>
</template>

<script>
  export default {
    name: "AdviceForm",
    methods: {
      data() {
        return {
          name: ''
        };
      }
    }
  };
</script>

When I try to add v-model for inputs I get an error saying Property or method "name" is not defined on the instance but referenced during render. I am sure the name is defined in the data property. Also, the <advice-form></advice-form> is placed within the #app div in blade page.

Can anyone help me figure out what is the problem?

like image 922
Tes3awy Avatar asked Oct 30 '19 08:10

Tes3awy


1 Answers

Change your script to the following and try not to use any reserved keywords.

new Vue({
    router,
    components: [AdviceForm],
}).$mount('#app');

<script>
  export default {
    name: "AdviceForm",
    // data should be function outside the 
    // methods and try a non reserved keyword.
    data: function() {
      return {
        form_name: ''
      };
    },
    methods: {
       // All the methods
    },
    components: []
  };
</script>
like image 119
Kalesh Kaladharan Avatar answered Nov 08 '22 02:11

Kalesh Kaladharan