VueJS 2(vue)组件中折叠内部的动态内容

ddrv8njm  于 2023-03-19  发布在  Vue.js
关注(0)|答案(2)|浏览(123)

我想在Collapse按钮/元素/下拉列表中创建动态内容。因为我会多次重复使用它。那么我如何在Collapse中传递组件呢?例如,Collapse中有一些DataTable,我可以在其中按“+”按钮,然后会出现另一个Collapse(我已经做好了)还有一个内容,我想用那些折叠,里面不会是table,但例如一个表单。我想优化它,并将其用于未来的崩溃与不同的内容。
我正在使用VueJS 2、BootstrapVue
这是我的CollapseSection.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>

如何优化它并使方法/操作工作(因为在每个崩溃中,数据和操作/方法都应该相互连接

zmeyuzjn

zmeyuzjn1#

如果collapseSection.vue内部的数据是动态的和未知的,最好使用slot。

<slot name="form"></slot>

然后使用

<CollapseSection select-text="Collapse ">
      <template v-slot:form>
          // your form goes here
          <form>             
          </form> 
     </template>
</CollapseSection>
li9yvcax

li9yvcax2#

因此,我使用了“命名插槽”,效果很好
这是我更新的CollapseSection.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>

相关问题