I want to make the dynamic content inside the Collapse button/element/dropdown. Because I will reuse it a lot. So how can I pass components inside the Collapse? For example there is some DataTable in the Collapse where I can press "+" button and another collapse would appear(I already made it) and there is another content, and I want to use those collapses, and inside of it wouldn't be a table, but for example a form. I want to Optimize it and use it for future collapses with different content.
I am using VueJS 2, BootstrapVue

这是我的折叠节.vue

<template>
  <div class="accordion" role="tablist">
    <b-button block v-b-toggle.accordion-1 class="collapse-btn" align-h="between">
      {{ selectText }}
      <b-icon :icon="visible ? 'caret-down' : 'caret-up'" class="icon"></b-icon>
    </b-button>
    <b-card no-body class="mb-1">
      <b-collapse id="accordion-1" v-model="visible" accordion="my-accordion" role="tabpanel">
        <b-card-body>
          <DataTable :labels="labels" :data="data" :showAdd="true" @add-item="addItem"/>
        </b-card-body>
      </b-collapse>
    </b-card>
//below is dynamic collapse when Click on "+" button
    <div v-for="(item, index) in items" :key="index">
      <b-button block v-b-toggle="'accordion-' + (index + 2)" class="collapse-btn">
        {{ item.name }}
        <b-icon :icon="visible ? 'caret-down' : 'caret-up'" class="icon"></b-icon>
      </b-button>
      <b-card no-body class="mb-1">
        <b-collapse :id="'accordion-' + (index + 2)" accordion="my-accordion" role="tabpanel">
          <b-card-body>
                some content
          </b-card-body>
        </b-collapse>
      </b-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "CollapseButton",
  components: {DataTable},
  props: {
    selectText: {
      type: String,
      default: () => "Select",
    },
  },
  data() {
    return {
      items: [],
      visible: false,
    }
  },
}
</script>
NewForm.vue //is just an example when I call/use it collapse component(only template, cuz script is 
<template>
     <CollapseSection select-text="Collapse ">

     </CollapseSection>
</template>
<script>
export default {
  name: "NewForm",
  components: {CollapseSection},
}
</script>

如何对其进行优化并使方法/操作正常工作(因为在每个折叠中,数据和操作/方法应该相互关联

推荐答案

因此,我使用了"命名槽",它工作得很好

这是我最新的Collip seSection.vue

<b-card no-body class="mb-1">
    <b-collapse id="accordion-1" v-model="visible" accordion="my-accordion" role="tabpanel">
      <div class="d-flex justify-content-end m-2">
        <SearchField class="order-2"></SearchField>
      </div>
      <b-card-body>
        <!--     CONTENT WOULD APPEAR INSIDE THIS SLOT      -->
        <slot name="content" :addItem="addItem"></slot>
      </b-card-body>
    </b-collapse>
</b-card>

NewForm.vue

<CollapseSection select-text="Select Images">
      <template #content="{ addItem }">
             <DataTable :fields="labels" :items="data" :showAdd="true">
                        <b-icon icon="plus" class="add-btn" @click="addItem(data[0].name)">
                </b-icon>
             </DataTable>
      </template>
</CollapseSection>

Javascript相关问答推荐

使用jQuery在 bootstrap 网格中添加/删除元素并防止删除第一个元素

为什么我的useDispatch挂钩在这里设置不正确?

使用reaction创建可排序的表不起作用

文件阅读器未读取一个文件

鼠标移动时更新画布

如何在JavaScript中通过一次单击即可举办多个活动

D3多线图显示1线而不是3线

从实时数据库(Firebase)上的子类别读取数据

基于变量切换隐藏文本

react—router v6:路由没有路径

如何将Openjphjs与next.js一起使用?

获取Uint8ClampedArray中像素数组的宽度/高度

如何在Angular17 APP中全局设置AXIOS

如何在每次单击按钮时重新加载HighChart/设置HighChart动画?

不能将空字符串传递给cy.containes()

本地库中的chartjs-4.4.2和chartjs-plugin-注解

在VS代码上一次设置多个变量格式

为什么在函数中添加粒子的速率大于删除粒子的速率?

打字脚本中方括号符号属性访问和拾取实用程序的区别

如何用javascript更改元素的宽度和高度?