【问题标题】:Is it possible to pass props from child component to template?是否可以将道具从子组件传递到模板?
【发布时间】:2021-05-04 08:17:47
【问题描述】:

这可能吗?

将道具从孩子发送给父母...

Child.vue

<template showLogo="false">
    ...
</template>

Parent.vue

<template>
  <div v-if="showLogo">
    <logo>
  </div>
  ....
</template>

【问题讨论】:

  • 你可以用事件来做。道具是单向的。 " 所有的 props 在子属性和父属性之间形成一个单向向下的绑定:当父属性更新时,它会向下流向子属性,而不是相反。这样可以防止子组件意外改变父属性状态,这会使您的应用程序的数据流更难理解。” ,引用官方文档。

标签: vue.js inertiajs


【解决方案1】:

这几乎是作用域插槽:https://vuejs.org/v2/guide/components-slots.html#Scoped-Slots

如您的示例:

parent.vue

<child>
  <template v-slot="{ showLogo }">
    <logo v-if="showLogo">
    </logo>
  </template>
</child>

这个视频对这个主题很有帮助:https://adamwathan.me/the-trick-to-understanding-scoped-slots-in-vuejs/

但我建议首先阅读有关插槽的内容,但要小心,因为它是一种更高级的模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多