简体   繁体   English

Vue使用道具将数据从父组件传递到子组件?

[英]Vue pass data from parent to child component using props?

How do i get topicCategories in the child component.如何在子组件中获取topicCategories I tried using states below but it is always the previous page topicCategories not the current page.我尝试使用下面的状态,但它始终是上一页topicCategories而不是当前页面。

Parent (post.vue)父级(post.vue)

export default class MyPost extends mixins(LocaleMixin) {
  Categories: any[] = [];
  topicCategories: any[] = [];

  async mounted() {
    this.loading = true;
    await this.$apiRequest(
      async () => {
        const {
          data: {
            id,
            topicCategories,
          },
        } = await this.$services.topic.fetchTopic(
          this.$route.params.slug,
          this.currentLocale.name
        );
        this.Categories = topicCategories as ICategory[];
        this.topicCategories = this.Categories.map((category: any) => category.id);
        this.$store.dispatch('app/setCurrentCategories', topicCategories);

      },
      () => {
        this.$nuxt.error({ statusCode: 404, message: 'err message' });
      }
    );
    this.loading = false;
  }

}

Child Component (readnext.vue)子组件(readnext.vue)

export default class ReadNext extends mixins(LocaleMixin) {
  offset = 0;
  total = 0;
  topics = [];

async fetch() {
    await this.$apiRequest(async () => {
 
    if(this.$store.state.app.currentCategories.length) {

        const categories = this.$store.state.app.currentCategories.map((category: any) => category.id);
      
        const { data } = await this.$services.topic.fetchReadNext(
            categories,
            this.currentLocale.name,
            10,
            this.offset * 10
        );

        //@ts-ignore
        this.topics.push(...data.rows);

        this.total = data.count;

        this.offset += 10;
        }
    });
  }
}

How do i get this.topicCategories from the parent to the child component without using states?如何在不使用状态的情况下从父组件获取this.topicCategories到子组件?

You need to declare topicCategories as a prop and then pass its value into a template.您需要将 topicCategories 声明为 prop,然后将其值传递给模板。 Here is how to do it: Child:操作方法如下: 孩子:

import { Prop } from 'vue-property-decorator';
export default class ReadNext extends mixins(LocaleMixin) {
   @Prop({default: [], required: true}) topicCategories!: any[];
   ...
}

Parent:家长:

<child :topicCategories="topicCategories" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用道具的情况下将数据从父组件传递到子组件 - How to pass data from parent to child component without using props 如何在vue中使用props将axios响应对象从父组件传递到子组件 - How to pass axios response object from a parent component to child component using props in vue 如何在Vue中将多个道具从父组件传递给子组件 - How to pass multiple props from parent to child component in Vue vue props 从父组件传递到子组件 - vue props passing from parent component to child 在Vue js中将数据从父组件传递给子组件 - Pass data from parent component to child of child in Vue js 将数据从子组件传递到父组件Vue js - Pass data from child component to parent component Vue js 在 vue 中使用 Props 从子模态数据更新父数据 - Update Parent Data from Child Modal Data using Props in vue 我可以将道具传递给子组件并在Vue中使用父组件中的数据吗 - Can I pass props to a child component and use the data in the parent component in Vue Vue.js如何使用Vue-Router将道具/参数从父组件传递到子组件 - Vue.js how to pass props/params from parent to child component with vue-router 使用Vue中的单击事件(数据)将数据从子组件传递到父组件 - Pass data from child to parent component with click event(data) in Vue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM