1
votes

I am trying to replicate the vue tutorial example (found here: https://v3.vuejs.org/guide/component-basics.html#passing-data-to-child-components-with-props ), but with no success. Below is my code. I have a view called TA.vue, that i would like to import the component into and render. Any ideas what I am doing wrong?

TA.vue (the view):

<template id="front">
    <b-container style="margin-top: 9rem;">
        <b-row>
            <div id="blog-posts-events-demo" class="demo">
                <div>
                    <blog_post
                        v-for="post in posts"
                        :key="post.id"
                        :title="post.title"
                    ></blog_post>
                </div>
            </div>
        </b-row>

    </b-container>

</template>

<script>
import blog_post from '../components/blog_post' // import the Header component
  export default {
    name: 'talent-acquisition',
    components: {
        blog_post
    }
  }
</script>

blog_post component:

Vue.component('blog_post', {
    el: '#front',
    
    data() {
        return {
            posts: [
                { id: 1, title: 'My journey with Vue'},
                { id: 2, title: 'Blogging with Vue'},
                { id: 3, title: 'Why Vue is so fun'}
            ]
        }
    },
  props: ['title'],
  template: `
    <div class="blog_post">
      <h4>{{ title }}</h4>
    </div>
  `
})

app.mount('#blog-posts-events-demo')

EDIT: After following Amaarrockz suggestion, I have the following error:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <BlogPost> at src/components/blog_post.vue
       <TalentAcquisition>
         <App> at src/App.vue
           <Root>
1

1 Answers

1
votes

The thing is you have array 'posts' defined in your child component('blog_post') which you are trying to iterate from the parent. The better implementation would be to define the array in the parent component . Please find the modifications below

TA.vue

<template id="front">
    <b-container style="margin-top: 9rem;">
        <b-row>
            <div id="blog-posts-events-demo" class="demo">
                <div>
                    <blog_post
                        v-for="post in posts"
                        :key="post.id"
                        :title="post.title"
                    ></blog_post>
                </div>
            </div>
        </b-row>

    </b-container>

</template>

<script>
import blog_post from '../components/blog_post' // import the Header component
  export default {
    name: 'talent-acquisition',
    components: {
        blog_post
    },
data() {
        return {
            posts: [
                { id: 1, title: 'My journey with Vue'},
                { id: 2, title: 'Blogging with Vue'},
                { id: 3, title: 'Why Vue is so fun'}
            ]
        }
    },
  }
</script>

blog_post component:

Vue.component('blog_post', {
    el: '#front',
  props: ['title'],
  template: `
    <div class="blog_post">
      <h4>{{ title }}</h4>
    </div>
  `
})

app.mount('#blog-posts-events-demo')