在隐藏状态下清除自举模态的身体

时间:2019-02-10 06:57:11

标签: vuejs2 nuxt.js bootstrap-vue

我有一个使用Bootstrap Vue的Nuxt应用程序。

我有一个可重用的Modal组件

components / BaseModal.vue

<template>
  <b-modal id="base_modal" :title="title" size="md" @hidden="hide">
    <slot name="body"></slot>
    <hr>    
    <BaseButton class="btn" variant="outline-secondary" @click="close" buttonText="Cancel"/>      
    <slot name="footer"></slot>
  </b-modal>
</template>

<script>
export default {
  name: 'BaseModal',
  props: {
    title: { type: String, required: true },
    size: { type: String, default: "lg" }
  }, 
  methods: {  
    close() {
      this.$root.$emit('bv::hide::modal', 'base_modal');
    }, 
    hide() {
      // Clear slot body contents here
    }
  }
};
</script>

我通过使用命名范围渲染html元素来调用此组件:

<template>
  <BaseButton @click="showModal" buttonText="Show Modal"/>

  <BaseModal title="Add Recommendation" size="md">
    <div slot="body">
      <b-form @submit.prevent="submit" autocomplete="off">
        <b-card-body>
          <b-row>
            <b-col><BaseTextInput v-model.trim="name" label="Headline"/></b-col>
          </b-row>
        </b-card-body>
      </b-form>
    </div>
    <BaseButton class="float-right btn" variant="outline-primary" @click="submit" buttonText="Save" slot="footer"/>
  </BaseModal>
</template>

<script>
  export default {
    name: 'FormInput',
    data() {
      return {
        name: ""
      } 
    },
    methods: {
      showModal: {
        this.$root.$emit('bv::show::modal','base_modal');
      },
      submit() {
        // dispatch creation
      }
    }
  }
</script>

隐藏时,如何重置/清除模态主体的内容,以便在触发showModal时它将再次重新呈现内容?

1 个答案:

答案 0 :(得分:1)

可以考虑以下解决方案:

选项1:从模式组件发出事件

1)模态关闭后,在Modal组件中发出自定义事件:

Modal.vue

<template>
  <b-modal id="base_modal" title="Modal example" @hidden="hide" >
    <slot name="body"></slot>
    <slot name="footer"></slot>
  </b-modal>
</template>

<script>
export default {
  name: "BaseModal",
  methods: {
    hide() {
      this.$emit('hide')
    }
  }
};
</script>

2)然后在hide事件触发后在父组件中,可以像这样清除表单值:

App.vue

<template>
  <div>
    <b-button @click="showModal" ref="btnShow">Open Modal</b-button>
    <ModalExample @hide="hideModal">
      <div slot="body">
        <form @submit.stop.prevent="handleSubmit">
          <b-form-input type="text" placeholder="Enter your name" v-model="name"></b-form-input>
        </form>
      </div>
    </ModalExample>
  </div>
</template>

<script>
import ModalExample from "./components/ModalExample.vue";

export default {
  components: {
    ModalExample
  },
  data() {
    return {
      name: ""
    };
  },
  methods: {
    showModal() {
      this.$root.$emit("bv::show::modal", "base_modal");
    },
    hideModal() {
      this.name = ''; //clear form values
    }
  }
};
</script>

Here is a demo演示了这种方法。

选项2:利用范围内的插槽

模态组件可以通过scoped slots访问数据:

ModalExample.vue

<template>
  <b-modal id="base_modal" title="Modal example" @hidden="hide" >
    <slot :formData="formData" name="body"></slot>
    <slot name="footer"></slot>
  </b-modal>
</template>

<script>
export default {
  name: "ModalExample",
  data() {
    return {
      formData : {}
    }
  },
  methods: {
    hide() {
      //clear form fields
      for (const name of Object.keys(this.formData)) {
        this.formData[name] = ""
      }
    }
  }
};
</script>

App.vue

<template>
  <div>
    <b-button @click="showModal" ref="btnShow">Open Modal</b-button>
    <ModalExample @hide="hideModal">
      <template slot="body" slot-scope="{formData}">
        <form @submit.stop.prevent="handleSubmit">
          <b-form-input
            type="text"
            placeholder="Enter your first name"
            v-model="formData.firstname"
          ></b-form-input>
          <b-form-input
            type="text"
            placeholder="Enter your last name"
            v-model="formData.lastname"
          ></b-form-input>
        </form>
      </template>
    </ModalExample>
  </div>
</template>

<script>
import ModalExample from "./components/ModalExample.vue";

export default {
  components: {
    ModalExample
  },
  data() {
    return {
    };
  },
  methods: {
    showModal() {
      this.$root.$emit("bv::show::modal", "base_modal");
    }
  }
};
</script>