Home  >  Article  >  Web Front-end  >  How to use popup layer component in uniapp

How to use popup layer component in uniapp

WBOY
WBOYOriginal
2023-07-04 12:54:067850browse

Title: How to use pop-up layer components in uniapp

Introduction:
In uniapp development, pop-up layer components are often used to implement some pop-up windows, prompt boxes and other functions. This article will introduce how to use the popup layer component in uniapp and provide relevant code examples.

1. Use the pop-up layer component officially provided by uniapp
Uniapp officially provides a pop-up layer component called uni-popup, which can be used to achieve various types of pop-up layer effects. First, we need to import the uni-popup component into the page or component that needs to use the popup layer.

Code example:

Import the uni-popup component in the page or component:

<template>
  <view>
    <uni-popup :show="isShowPopup" position="bottom">
      <!-- 弹出层内容 -->
    </uni-popup>
  </view>
</template>

<script>
  import uniPopup from '@/components/uni-popup/uni-popup.vue';

  export default {
    components: {
      uniPopup
    },
    data() {
      return {
        isShowPopup: false
      };
    }
  };
</script>

In the above code, we first use the uni-popup component in the template, and Use the :show attribute to control the display and hiding of the pop-up layer. isShowPopup is a Boolean type variable. Control the display and hiding of the pop-up layer by controlling the value of this variable. You can also set the position of the pop-up layer through the position attribute. You can choose "top", "bottom", "left", "right", etc.

In the pop-up layer component, we can customize the content that needs to be displayed. We only need to add the content that needs to be displayed in the uni-popup tag.

2. Custom pop-up layer component
In some scenarios, we may need a more customized pop-up layer effect. In this case, we can customize a pop-up layer component to achieve it. Below we will take the custom pop-up layer as an example to introduce how to customize the pop-up layer component in uniapp.

Code example:

Import custom popup layer component into the page or component:

<template>
  <view>
    <!-- 按钮 -->
    <button @click="showCustomPopup">点击弹出自定义弹出层</button>

    <!-- 自定义弹出层组件 -->
    <custom-popup :show="isShowCustomPopup" @close="closeCustomPopup">
      <!-- 弹出层内容 -->
    </custom-popup>
  </view>
</template>

<script>
  import customPopup from '@/components/custom-popup.vue';

  export default {
    components: {
      customPopup
    },
    data() {
      return {
        isShowCustomPopup: false
      };
    },
    methods: {
      showCustomPopup() {
        this.isShowCustomPopup = true;
      },
      closeCustomPopup() {
        this.isShowCustomPopup = false;
      }
    }
  };
</script>

Custom popup layer component custom-popup.vue code example:

<template>
  <view v-show="show">
    <view class="popup-bg" @click.stop="close"></view>
    <view class="popup-content">
      <!-- 弹出层内容 -->
      <slot></slot>
    </view>
  </view>
</template>

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

<style>
  /* 弹出层样式 */
  .popup-bg {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.5);
  }

  .popup-content {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  }
</style>

In the above code, we first use the custom pop-up layer component custom-popup in the template, and control the display and hiding of the pop-up layer through the :show attribute. isShowCustomPopup is also a Boolean type variable. By controlling the variable The value controls the display and hiding of the popup layer. In the custom popup layer component custom-popup.vue, we use slots to customize the content of the popup layer.

Conclusion:
Through the above introduction, we can see that using the pop-up layer component in uniapp is very simple. You can choose to use the pop-up layer component officially provided by uniapp or customize the pop-up layer component according to actual project needs to achieve pop-up layer effects of different styles and functions. I hope this article will help you use popup layer components in uniapp development.

The above is the detailed content of How to use popup layer component in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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