简体   繁体   English

如何从 api 传递数据作为 vuejs 中的道具

[英]how to pass data from api as prop in vuejs

I am getting data from api which I want to pass as prop to a child component我从 api 获取数据,我想将其作为道具传递给子组件

Parent component父组件

<template>
    <div class="dashboard">
        <items name="Admins" value={{data.items.hubs}} bgColor="#a80c0c" />
    </div>
</template>
import Items from './Items.vue'
import { Admin } from '@/services/AdminService';
export default {
    name: "AdminDashboard",
    components: {
        Items
    },

    setup(){

    onMounted(() => {
      showLoader(true);
        Admin.getDashboardItems()
        .then((response) => {
            data.items = response.data.data
        })
        .catch((error) => {
        })
        .finally(() => {
          showLoader(false);
        });
    });
        return {
            data
        }

    }
}

I have gotten the value I need from the api and passed it to data.items我从 api 获得了我需要的值并将其传递给 data.items

If i display it on the parent component.如果我在父组件上显示它。

It works fine but on the child component它工作正常,但在子组件上

it does not work这没用

Child Component子组件

<template>
    <div class="col-md-3">
        <div class="items" :style="{ backgroundColor: bgColor }">
            <div class="d-flex space-between">
                <div></div>
                <div>
                    <h5>{{ value }}</h5>
                    <span>{{ name }}</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

export default {
    name: "Items",
    props: ["bgColor", "value", "name"]
}
</script>

The child components display {{data.items.hubs}} instead of the value of hub子组件显示 {{data.items.hubs}} 而不是 hub 的值

The data from the api来自 api 的数据

{"data":{"users":1,"incubatees":1,"hubs":2,"investors":1,"events":0,"admins":3,"programs":0}}

After some research经过一番研究

I found out I was not supposed to use {{}}我发现我不应该使用 {{}}

instead I should do it this way on the parent component相反,我应该在父组件上这样做

<items name="Hubs" :value="data.items.hubs" bgColor="#d79a2b" />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM