3

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>
1
  • No.. my HomeComponent contain some html which i did not include it here Commented Jan 1, 2019 at 20:32

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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>

Comments

1

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

Comments

-1

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.

Comments

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.