欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3如何自定義message彈窗

 更新時(shí)間:2024年04月27日 16:02:14   作者:jjw_zyfx  
這篇文章主要介紹了vue3如何自定義message彈窗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1、定義一個(gè)Message.vue組件

其內(nèi)容如下

<template>
  <Transition name="down">
    <div class="xtx-message" :style="style[type]" v-show="visible">
      <!-- 上面綁定的是樣式 -->
      <!-- 不同提示圖標(biāo)會(huì)變 :class="{'icon-warning':true}" :class="['icon-warning']" -->
      <!--這個(gè)需要用到https://www.iconfont.cn/的圖片庫(kù)-->
<!--      <i class="iconfont" :class="[style[type].icon]"></i>-->
      <span class="text">{{text}}</span>
    </div>
  </Transition>
</template>
<script>
import { ref } from 'vue'
export default {
  name: 'Message',
  props: {
    type: {
      type: String,
      default: 'warn'
    },
    text: {
      type: String,
      default: ''
    }
  },
  setup () {
    // 定義一個(gè)對(duì)象,包含三種情況的樣式,對(duì)象key就是類型字符串
    const style = {
      warn: {
        icon: 'icon-warning',
        color: '#E6A23C',
        backgroundColor: 'rgb(253, 246, 236)',
        borderColor: 'rgb(250, 236, 216)'
      },
      error: {
        icon: 'icon-shanchu',
        color: '#F56C6C',
        backgroundColor: 'rgb(254, 240, 240)',
        borderColor: 'rgb(253, 226, 226)'
      },
      success: {
        icon: 'icon-queren2',
        color: '#67C23A',
        backgroundColor: 'rgb(240, 249, 235)',
        borderColor: 'rgb(225, 243, 216)'
      }
    }
    // 控制元素顯示隱藏
    // const visible = ref(false)
    const visible = ref(true)// 這個(gè)感覺(jué)可以不用,即不用v-show
    // onMounted(() => {
    //   visible.value = true
    // })
    return { style, visible }
  }
}
</script>
<style scoped lang="less">
.down {
  &-enter {
    &-from {
      transform: translate3d(0,-75px,0);
      opacity: 0;
    }
    &-active {
      transition: all 0.5s;
    }
    &-to {
      transform: none;
      opacity: 1;
    }
  }
}
.xtx-message {
  width: 300px;
  height: 50px;
  position: fixed;
  z-index: 9999;
  left: 50%;
  margin-left: -150px;
  top: 25px;
  line-height: 50px;
  padding: 0 25px;
  border: 1px solid #e4e4e4;
  background: #f5f5f5;
  color: #999;
  border-radius: 4px;
  i {
    margin-right: 4px;
    vertical-align: middle;
  }
  .text {
    vertical-align: middle;
  }
}
</style>

2、創(chuàng)建一個(gè)叫Message.js的文件

其內(nèi)容如下:

// 提供一個(gè)能夠顯示Message組件的函數(shù)
// 這個(gè)函數(shù)將來(lái):導(dǎo)入直接使用,也可以掛載在vue實(shí)例原型上

import { createVNode, render } from 'vue'
import HelloWorld from "@/components/Message.vue";

// DOM容器
const div = document.createElement('div')
div.setAttribute('class', 'xtx-msssage-container')
document.body.appendChild(div)

// 定時(shí)器標(biāo)識(shí)
let timer = null

export default ({ type, text }) => {
    // 渲染組件
    // 1. 導(dǎo)入消息提示組件
    // 2. 將消息提示組件編譯為虛擬節(jié)點(diǎn)(dom節(jié)點(diǎn))
    // createVNode(組件,屬性對(duì)象(props))
    const vnode = createVNode(HelloWorld, { type, text })
    // 3. 準(zhǔn)備一個(gè)裝載消息提示組件的DOM容器
    // 4. 將虛擬節(jié)點(diǎn)渲染再容器中
    // render(虛擬節(jié)點(diǎn),DOM容器)
    render(vnode, div)
    // 5. 3s后銷毀組件
    clearTimeout(timer)
    timer = setTimeout(() => {
        render(null, div)
    }, 3000)
}

3、在app.vue中導(dǎo)入并使用

<template>
  <div class="btn" @click="btn">點(diǎn)擊彈出消息提示框</div>
</template>


<script setup>
import Message from "@/Message";
import {onMounted} from "vue";
const btn = ()=>{
  Message({type:'warn', text:"hello world"})
}

// 這種方式雖然也可以但是不推薦
// onMounted(()=>{
//     this.$message({type:'warn', text:"hello world"})
// })

</script>
<style lang="less" scoped>
.btn{
  width: 300px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  background-color: deeppink;
}
</style>

4、效果如下

當(dāng)點(diǎn)擊左邊按鈕后會(huì)彈出一個(gè)消息提示框,3秒后關(guān)閉

5、如果想使用this.$message這種方式

則需定義一個(gè)UI.js文件用來(lái)掛載Message.js

其內(nèi)容如下:

import Message from "@/Message";
export default {
    install (app) {
        // 定義一個(gè)原型函數(shù)
        app.config.globalProperties.$message = Message
    }
}

6、在main.js中導(dǎo)入并使用use掛載到app上

如下所示:

import { createApp } from 'vue'
import App from './App.vue'
import UI from "@/UI";
// 導(dǎo)入自己UI組件庫(kù)
// import UI from './UI'

// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(UI).mount('#app')

這種方式的缺點(diǎn)是還是得在選項(xiàng)式上才方便使用,在組合式方式上不推薦

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論