Lubomir Stankov
Lubomir Stankov

Reputation: 33

VueJS, pass data from component to another

I make something new for me and i have problem.. So lets explain little more..

I have Component with name components/HomeComponent.vue

Here is:

HomeComponent.vue

<script>
export default {
  name: "HomeComponent",
  data() {
    posts: [
      { title: "Hello", body: "Some text" },
      { title: "Hello", body: "Some text" },
      { title: "Hello", body: "Some text" },
      { title: "Hello", body: "Some text" }
    ];
  }
};
</script>

and i have my "view" views/Home.vue

Home.vue

<template>
  <!-- Blog Entries Column -->
  <div class="col-md-8">
    <h1 class="my-4">Статии</h1>

    <!-- Blog Post -->
    <div class="card mb-4" v-for="post in posts">
      <div class="card-body">
        <h2 class="card-title">{{ post.title }}</h2>
        <p class="card-text">{{ post.body }}</p>
        <a href="#" class="btn btn-primary">Read More &rarr;</a>
      </div>
      <div class="card-footer text-muted">
        Posted on January 1, 2017 by
        <a href="#">xxx</a>
      </div>
    </div>
  </div>
</template>

So i want to access posts in my Home.vue and make for loop.. How to do that? Thanks in advice! :)

<script>
// @ is an alias to /src
import HomeComponent from "@/components/HomeComponent.vue";

export default {
  name: "home",
  components: {
    HomeComponent
  },
};
</script>

Upvotes: 3

Views: 8828

Answers (4)

Teddy McZieuwa
Teddy McZieuwa

Reputation: 377

You would have to pass down your data as props to the Home component. More information can be found here. But here is a quick fix to your problem.

Comp.vue

<template>
  <!-- Blog Entries Column -->
  <div class="col-md-8">
    <h1 class="my-4">Статии</h1>

    <!-- Blog Post -->
    <div class="card mb-4" v-for="post in posts">
      <div class="card-body">
        <h2 class="card-title">{{ post.title }}</h2>
        <p class="card-text">{{ post.body }}</p>
        <a href="#" class="btn btn-primary">Read More &rarr;</a>
      </div>
      <div class="card-footer text-muted">
        Posted on January 1, 2017 by
        <a href="#">xxx</a>
      </div>
    </div>
  </div>
</template>

<script>
 export default {
   props:['posts']
 }
</script>

HomePage.vue

<template>
  <comp :posts="posts"></comp>
</template>

<script>
import Comp from './components/Comp.vue'
export default {
  name: "HomeComponent",
  components: {
    'comp': Comp
  },
  data() {
    posts: [
      { title: "Hello", body: "Some text" },
      { title: "Hello", body: "Some text" },
      { title: "Hello", body: "Some text" },
      { title: "Hello", body: "Some text" }
    ];
  }
};
</script>

Upvotes: 2

Yasin Tazeoglu
Yasin Tazeoglu

Reputation: 1014

HomeComponent.vue

<template>
  <div>
    <slot :posts="posts"></slot>
  </div>
</template>
<script>
 export default {
 name: "HomeComponent",
 data() {
  posts: [
    { title: "Hello", body: "Some text" },
    { title: "Hello", body: "Some text" },
    { title: "Hello", body: "Some text" },
    { title: "Hello", body: "Some text" }
 ];
}
};
</script>

Home.vue

<template>
    <div>
        <home-component slot-scope="{ posts }">
            <div class="card mb-4" v-for="post in posts">
               <div class="card-body">
                 <h2 class="card-title">{{ post.title }}</h2>
                 <p class="card-text">{{ post.body }}</p>
                 <a href="#" class="btn btn-primary">Read More &rarr;</a>
               </div>
               <div class="card-footer text-muted">
                 Posted on January 1, 2017 by
                 <a href="#">xxx</a>
               </div>
             </div> 
        </home-component>
    </div>
</template>
<script>
import HomeComponent from "@/components/HomeComponent.vue";
export default {
  name: "home",
  components: {
    HomeComponent
  },
};
</script>
  

https://medium.com/binarcode/understanding-scoped-slots-in-vue-js-db5315a42391 https://v2.vuejs.org/v2/guide/components-slots.html#Scoped-Slots

Upvotes: 1

Muthu
Muthu

Reputation: 19

You have defined data which is specific to the component. If you want to spread/pass data into different component/views, Kindly keep data in app.vue and access data directly inside view/component. Another way to access data in a centralized location, You can use VueX (data store), its bit advance level to manipulate huge capacity of data.

Upvotes: -1

ibn_Abubakre
ibn_Abubakre

Reputation: 112

Data is passed to child components using props. You can use emit to pass it to the parent component.

data() {
  posts: [
    { title: "Hello", body: "Some text" },
    { title: "Hello", body: "Some text" },
    { title: "Hello", body: "Some text" },
    { title: "Hello", body: "Some text" }
  ];
},
mounted () {
  this.$emit('posts', this.posts);
}
  

Then on Home.vue, you listen for a change in event using this

<template>
  <div>
    <HomeComponent v-on:posts="getPosts($event)"></HomeComponent>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        posts: []
      }
    },
    methods: {
      getPosts(e) {
        this.posts = e; 
      }
    }
  }
</script>

Upvotes: 2

Related Questions