如何与子组件共享父道具?

时间:2019-01-04 16:53:25

标签: javascript vue.js vuejs2

我试图弄清楚如何将父项中的prop传递给子项。

如果我在子组件标签中添加带有#id的prop属性,则效果很好,例如 Image cid =“ 488484-49544894-584854” ,但我想使用相同的 cid 在一个位置(父母)-这可能吗?

父组件和子组件具有相同的安装和数据功能。 cid 被传递到 contentDeliveryUrl 以收集数据。

App.vue

<template>
  <div id="app" class="container" cid="7e4301de-9c6e-4fab-9e68-3031b94d662d">

    <Images cid="same as parent div" />

    <Video cid="same as parent div" />

    <TextType cid="same as parent div" />

    <Card cid="same as parent div" />

  </div>
</template>

<script>
  import axios from 'axios';
  import Images from "./components/Images";
  import Video from "./components/Video";
  import TextType from "./components/TextType";
  import Card from "./components/Card";

  export default {
    name: 'app',
    props: ["cid"],
    components: {
      Images,
      Video,
      TextType,
      Card
    },
    mounted() {
      axios({method: "GET", "url": this.contentDeliveryUrl}).then(result => {
          // eslint-disable-next-line
          this.content = amp.inlineContent(result.data)[0];
          console.log(this.content)
        }, error => {
          console.error(error);
        });
    },
    data() {
      return {
        contentDeliveryUrl: 'https://c1.adis.ws/cms/content/query?fullBodyObject=true&query=%7B%22sys.iri%22%3A%22http%3A%2F%2Fcontent.cms.amplience.com%2F${this.cid}%22%7D&scope=tree&store=testing',
        content: []
      }
    }
  }
</script>

Image.vue

<template>

<div v-if="content.image">

</div>

</template>

<script>
  import axios from 'axios';

  export default {
    props: ["cid"],
    name:'Images',

    mounted() {
      axios({method: "GET", "url": this.contentDeliveryUrl}).then(result => {
          // eslint-disable-next-line
          this.content = amp.inlineContent(result.data)[0];
          console.log(this.content)
        }, error => {
          console.error(error);
        });
    },
    data() {
      return {
        contentDeliveryUrl: 'https://c1.adis.ws/cms/content/query?fullBodyObject=true&query=%7B%22sys.iri%22%3A%22http%3A%2F%2Fcontent.cms.amplience.com%2F${this.cid}%22%7D&scope=tree&store=testing',
        content: []
      }
    },
}
</script>

使用Vue Devtools返回的所有组件的数据均未定义。

1 个答案:

答案 0 :(得分:1)

例如,添加一个名为cidItem的数据属性,并将其绑定到道具,如下所示:

<template>
  <div id="app" class="container" :cid="cidItem">

    <Images :cid="cidItem" />

    <Video :cid="cidItem"  />

    <TextType :cid="cidItem"  />

    <Card :cid="cidItem"  />

  </div>
</template>

<script>
  import axios from 'axios';
  import Images from "./components/Images";
  import Video from "./components/Video";
  import TextType from "./components/TextType";
  import Card from "./components/Card";

  export default {
    name: 'app',
    props: ["cid"],
    components: {
      Images,
      Video,
      TextType,
      Card
    },
    mounted() {
      axios({method: "GET", "url": this.contentDeliveryUrl}).then(result => {
          // eslint-disable-next-line
          this.content = amp.inlineContent(result.data)[0];
          console.log(this.content)
        }, error => {
          console.error(error);
        });
    },
    data() {
      return {
        contentDeliveryUrl: 'https://c1.adis.ws/cms/content/query?fullBodyObject=true&query=%7B%22sys.iri%22%3A%22http%3A%2F%2Fcontent.cms.amplience.com%2F${this.cid}%22%7D&scope=tree&store=testing',
        content: [],
        cidItem:'7e4301de-9c6e-4fab-9e68-3031b94d662d'
      }
    }
  }
</script>

由于您的组件具有相同的结构,因此我建议使用mixins,因此请创建一个名为myMixins.js的文件,并在其中添加以下代码:

const myMixins = {
 props:['cid'],
  mounted() {
    axios({
      method: "GET",
      "url": this.contentDeliveryUrl
    }).then(result => {
      // eslint-disable-next-line
      this.content = amp.inlineContent(result.data)[0];
      console.log(this.content)
    }, error => {
      console.error(error);
    });
  },
  data() {
    return {
      contentDeliveryUrl: 'https://c1.adis.ws/cms/content/query?fullBodyObject=true&query=%7B%22sys.iri%22%3A%22http%3A%2F%2Fcontent.cms.amplience.com%2F${this.cid}%22%7D&scope=tree&store=testing',
      content: []
    }
  }
}

export default mixins;

在每个组件内部添加:

    import myMixins from './myMixins'
    export default{
          ....
         mixins: [myMixin]
      }