vue注册全局组件

本文介绍了如何在Vue中进行全局组件注册,包括自定义弹窗组件的创建和使用函数控制组件的显示与隐藏。通过Vue.component方法,开发者可以方便地在整个应用中复用组件。
摘要由CSDN通过智能技术生成

vue注册全局组件 (Vue.component)

step1: 自定义弹窗组件

<template>
  <transition name="fade"
              v-if="true">
    <div class="dialog-page">
      <div class="dialog-box">
        <h3 class="title">{
  { title }}</h3>
        <span class="close"
              @click="close">X</span>
        <button type="primary"
                class="confirm-btn"
                @click="confirm">确定</button>
      </div>
    </div>
  </transition>
</template>

<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator';
@Component({})
export default class Dialog extends Vue {
  @Prop()
  title!: string;

  isShow = false;

  mounted() {
    window.addEventListener('keyup', this.close);  // 任意按键隐藏弹窗
    this.isShow = true;
  }

  close() {
    // this.isShow = false;
    this.$nextTick(() => {
      this.$emit('close');   // 触发父组件的close方法,即Dialog绑定的close方法
    });
  }

  confirm() {
    this.$emit('confirm');  // 触发父组件的confirm方法,即Dialog绑定的confirm方法
  }
}
</script>

<style lang="scss" scoped>
.dialog-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.6);
  z-index: 99999;

  .dialog-box {
    position: absolute;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    margin: auto;
    width: 700px;
    height: 400px;
    padding-top: 20px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 6px 0 #fff;

    .title {
      text-align: center;
      font-size: 22px;
      color: #333;
    }

    .close {
      position: absolute;
      right: 20px;
      top: 20px;
      cursor: pointer;
    }

    .confirm-btn {
      width: 80%;
      position: absolute;
      left: 10%;
      bottom: 50px;
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>

step2: 定义函数控制组件的展示与消失

import Vue from "vue";
import Dialog from "@/components/Dialog/Dialog.vue";  // 导入自定义组件

let vm:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值