javascript - 如何将模板(槽)从祖父组件传递给 VueJS 中的孙组件?

标签 javascript vue.js vuejs2 vue-component bootstrap-vue

我在 VueJS 2 中有一个案例,其中有一个 3 级组合的组件(祖 parent - child - 孙)我需要将模板/插槽从祖 parent 传递给孙子(这是一个 b-table 组件来自Bootstrap + Vue 库(https://bootstrap-vue.js.org/docs/components/table))。

祖父组件:

<template>
  <DataTable
    :tableFields="postFields"
    :tableService="posts"\
  />
</template>
<script>
  export default {
    name: 'Posts',
    components: {
      DataTable,
    },
</script>

子组件:

<template>
  <b-table
    :items="items"
    :fields="tableFields"
  />
</template>
<script>
  export default {
    name: 'Posts',
    props: {
      tableFields: {type: Array, required: true},
      tableService: {type: Object, required: true},
    },
    components: {
      DataTable,
    },
</script>

孙组件:

这个是<b-table>来自 Bootstrap + Vue ( https://bootstrap-vue.js.org/docs/components/table )

正如 Bootstrap + Vue 文档所代表的那样,我可以传递 <template>下降到 <b-table>但我想先把它从祖 parent 传给 child ,然后再从 child 传给 <b-table> .

最佳答案

@ruy-garcia 我使用祖 parent 中定义的属性解决了类似的问题。此解决方案仅在您有一个要在其中更新一些属性的插槽时才有效,因此它不那么灵活。

// Grandparent
<app-table
    :tableFooterLink="{
        link: {name: 'new-player'},
        text: 'Create New Player'
    }"
>

然后在子组件中检查该属性。

// Child component
<vue-good-table>
    <template v-if="tableFooterLink !== undefined" slot="table-actions">
        <router-link class="create-new-player-btn" :to="{name: 'new-player'}">
            {{ tableFooterLink.text }}
        </router-link>
    </template>
</vue-good-table>

export default {
    // stuff
    props: {
        tableFooterLink: {
            type: Object
        }
    }
}

关于javascript - 如何将模板(槽)从祖父组件传递给 VueJS 中的孙组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221802/

相关文章:

vue.js - 使用 Vuetify 的可滚动数据表

css - nuxt 样式不会应用于生产模式

javascript - 使用谷歌工作表脚本在列中查找空单元格

javascript - 选择框应超出对话框的边缘

javascript - 具有动态 templateUrl 和 Transclude 的 Angular 指令

symfony - 从 VueJs 组件中受益于 Symfony/Twig i18n

javascript - 如何调用 Vuejs 实例声明的函数?

javascript - 选择后更改复选框标签

vue.js - 使用 Axios 进行 POST 到 DRF

vue.js - VueJS : Computed Property Is Calculated Before Created in Component?