1

I created two components with Vue.js and Laravel (FormComponent and ListComponent). In the form I should enter a movie name and then when I click the input button I should see the new movie in the list of movies that is located in ListComponent... however I am unable to do that. I alert the new movie correctly but I cannot see the new movie in the list.

FormComponent:

<template>
    <div class="container">
        <form>
            <div class="form-group">
                <label for="movie">Movie:</label>
                <input type="text" class="form-control" v-bind:placeholder="movie_default" v-model="movie_default" id="movie">
            </div>
            <input type="button" value="Submit" @click="add_movie()">
        </form>
    </div>
</template>

<script>

    export default {
        mounted() {
            console.log('Form Component mounted.')
        },
        data() {
            return {
                movie_default: 'Rainman'
            }
        },
        methods: {
            add_movie () {
                alert(this.movie_default);
            }
        }
    }
</script>

ListComponent:

<template>
    <div class="container">
        <ol>
            <li v-for="movie in movie_list" :key="movie">
                {{ movie }}
            </li>
        </ol>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('List Component mounted.')
        },
        data() {
            return {
                movie_list: [ 'Titanic', 'Profondo rosso']
            }
        }
    }
</script>

Tables.blade.php

<div id="app">
    <div class="row">
        <div class="col-sm-6">
            <form-component></form-component>
        </div>
        <div class="col-sm-6">
            <list-component></list-component>
        </div>
    </div>
</div>

1 Answer 1

3

You have some different ways to solve your problem. I would suggest you the easiest for the result I think you want to accomplish.

Create a third component my-form, that looks like this:

       myForm.vue
         <template>
           <div class="row">
             <div class="col-sm-6">
                <form-component @new-movie="addNewMovie"></form-component>
             </div>
             <div class="col-sm-6">
                <list-component :movies="movies_list"></list-component>
             </div>
            </div>
         </template>

    <script>
        export default {
        name: "myForm.vue",

        data() {

            return {

                movies_list: [],
                new_movie: ""
            }
        },
        methods: {
         addNewMovie(movie){
             this.movies_list.push(movie)
           }
       }
      }
    </script>

then edit your FormComponent to emit the new value on click:

methods: {
            add_movie () {
                alert(this.movie_default);
                this.$emit('new-movie',this.movie_default);
            }

and your ListComponent to use props instead of the state to display the movie list:

export default {
    props:['movies'],
    data() {
        return {
        }
    }

and update your v-for with to use the prop:

        <li v-for="movie in movies" :key="movie">

Another way would be to use a centralized store (vuex) to share the state between components

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

4 Comments

I tried in your way but I cannot see the movies in the list. If I click 3 times the button I see three numbers with empty values in the list.
No I solved... I forgot the {{ movie }} inside the li tag
what is the meaning of "<form-component @new-movie="addNewMovie"></form-component>"... I don't understand this sintax.
ciao Michele. So, @ is a shorthand for v-on: so basically is listening for an event. new-movie, is the event name that we defined in the form component (this.$emit('new-movie')). and addNewMovie is the name of the method to add a movie. You use it without (args) because the arguments get automatically injected in your function. Hope this helps!

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.