Home > Web Front-end > Vue.js > Vue component development: Modal box component implementation method

Vue component development: Modal box component implementation method

WBOY
Release: 2023-11-24 08:26:35
Original
1328 people have browsed it

Vue component development: Modal box component implementation method

Vue component development: Modal box component implementation method

In web applications, the modal box is a common UI control that can be used to display some Important content, such as prompt information, warning information, prompting users to perform certain operations, etc. This article will introduce how to use the Vue framework to develop a simple modal box component and provide code examples for reference.

  1. Component structure

First we need to define a modal box component, including HTML structure, style and logical functions. Components usually have a parent component passing properties to child components, and the child components render UI based on the properties.

The following is the simplest HTML structure of a modal box:

<template>
  <div class="modal">
    <div class="modal-content">
      <!-- modal header -->
      <div class="modal-header">
        <h4>{{ title }}</h4>
        <button class="close-btn" @click="closeModal">&times;</button>
      </div>
      <!-- modal body -->
      <div class="modal-body">
        <slot></slot>
      </div>
    </div>
  </div>
</template>
Copy after login

Among them, the modal box is divided into the following areas:

  • Title area (modal header) , including a title and a close button.
  • The modal body is used to display the content that needs to be displayed in the modal box. The content can be transferred through slots.

We also need to define some basic styles to make the modal box look more beautiful. Only a simple style is provided here, and readers can define more complex styles according to their own needs.

.modal {
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0,0,0,0.4);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background-color: #fefefe;
  border-radius: 5px;
  box-shadow: 0 0 20px rgba(0,0,0,0.4);
  max-width: 600px;
  width: 70%;
  padding: 20px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-btn {
  font-size: 24px;
  font-weight: bold;
  color: #aaaaaa;
}
Copy after login
  1. Component Function

Now, we need to give some functionality to the modal box component. First, we need to define some properties to pass the modal's title and show/hide state. Through these properties, we can control the display and hiding of the modal box in the parent component.

export default {
  name: 'Modal',
  props: {
    title: {
      type: String,
      default: ''
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    closeModal() {
      this.$emit('close');
    }
  }
}
Copy after login

Here we define two attributes:

  • title: The title of the modal box.
  • show: Show/hide state of modal box.

In addition, we defined a closeModal method in the component to close the modal box. This method will be called when the user clicks the close button, and sends a close event to the parent component through the event dispatch mechanism to tell the parent component that the modal box needs to be closed.

Next, we need to add some logic to the template of the modal box component to show or hide the modal box based on the value of the show attribute.

<template>
  <div v-if="show" class="modal">
    <div class="modal-content">
      <!-- modal header -->
      <div class="modal-header">
        <h4>{{ title }}</h4>
        <button class="close-btn" @click="closeModal">&times;</button>
      </div>
      <!-- modal body -->
      <div class="modal-body">
        <slot></slot>
      </div>
    </div>
  </div>
</template>
Copy after login
  1. Using components

Now we have completed the development of the modal box component. If you want to use this component, you only need to introduce the component in the parent component and pass in the required properties.

<template>
  <div>
    <button @click="showModal">显示模态框</button>
    <Modal :title="title" :show="show" @close="closeModal">
      <p>这里是模态框中的内容</p>
    </Modal>
  </div>
</template>

<script>
import Modal from './Modal.vue';
export default {
  name: 'App',
  components: {
    Modal
  },
  data() {
    return {
      title: '这里是模态框标题',
      show: false
    };
  },
  methods: {
    showModal() {
      this.show = true;
    },
    closeModal() {
      this.show = false;
    }
  }
}
</script>
Copy after login
Copy after login

Here, we use the Modal component in the parent component and pass in the title and show attributes. The show attribute controls the display and hidden state of the modal box, and the title attribute controls the title of the modal box.

After clicking the "Show Modal Box" button, the modal box will be displayed. Click the close button and the modal box will be hidden.

  1. Summary

Through the introduction of this article, we have learned how to use the Vue framework to develop a simple modal box component. Components allow us to organize code logic together, making it easier to understand and manage. When we need to reuse a certain function, we can abstract the function into a component and then reference it where needed. This improves code reusability and maintainability.

The complete code is as follows:

Modal.vue

<template>
  <div v-if="show" class="modal">
    <div class="modal-content">
      <!-- modal header -->
      <div class="modal-header">
        <h4>{{ title }}</h4>
        <button class="close-btn" @click="closeModal">&times;</button>
      </div>
      <!-- modal body -->
      <div class="modal-body">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Modal',
  props: {
    title: {
      type: String,
      default: ''
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    closeModal() {
      this.$emit('close');
    }
  }
}
</script>

Copy after login

App.vue

<template>
  <div>
    <button @click="showModal">显示模态框</button>
    <Modal :title="title" :show="show" @close="closeModal">
      <p>这里是模态框中的内容</p>
    </Modal>
  </div>
</template>

<script>
import Modal from './Modal.vue';
export default {
  name: 'App',
  components: {
    Modal
  },
  data() {
    return {
      title: '这里是模态框标题',
      show: false
    };
  },
  methods: {
    showModal() {
      this.show = true;
    },
    closeModal() {
      this.show = false;
    }
  }
}
</script>
Copy after login
Copy after login

The above is the detailed content of Vue component development: Modal box component implementation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template