VueJS,将数据从组件传递到另一个

时间:2019-01-01 20:20:32

标签: javascript arrays vue.js vuejs2

我为我制造了一些新东西,但我遇到了问题。所以让我们多解释一下。

我的组件名称为components/HomeComponent.vue

这里是:

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>

我有自己的“观点” 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>

因此,我想访问我的Home.vue中的帖子并进行循环。 感谢您的建议! :)

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

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

4 个答案:

答案 0 :(得分:2)

使用prop将数据传递到子组件。您可以使用发射将其传递给父组件。

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);
}
  

然后在Home.vue上,您可以使用此方法监听事件的变化

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

答案 1 :(得分:1)

HomeComponent.vue

<!-- HTML -->
<table id="table">
  <thead>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </thead>
</table>

Home.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>

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

答案 2 :(得分:0)

您必须将数据作为道具传递给Home组件。可以找到更多信息here。但这是解决您的问题的快速方法。

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>

答案 3 :(得分:0)

您已定义了特定于组件的数据。如果您想将数据传播/传递到不同的组件/视图中,请将数据保留在app.vue中,并直接在视图/组件内部访问数据。在集中位置访问数据的另一种方法是,您可以使用VueX(数据存储),它的位高级级别可以处理大量数据。