亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3如何自定義message彈窗

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

1、定義一個Message.vue組件

其內(nèi)容如下

<template>
  <Transition name="down">
    <div class="xtx-message" :style="style[type]" v-show="visible">
      <!-- 上面綁定的是樣式 -->
      <!-- 不同提示圖標會變 :class="{'icon-warning':true}" :class="['icon-warning']" -->
      <!--這個需要用到https://www.iconfont.cn/的圖片庫-->
<!--      <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 () {
    // 定義一個對象,包含三種情況的樣式,對象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)// 這個感覺可以不用,即不用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)建一個叫Message.js的文件

其內(nèi)容如下:

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

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)

// 定時器標識
let timer = null

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

3、在app.vue中導入并使用

<template>
  <div class="btn" @click="btn">點擊彈出消息提示框</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、效果如下

當點擊左邊按鈕后會彈出一個消息提示框,3秒后關閉

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

則需定義一個UI.js文件用來掛載Message.js

其內(nèi)容如下:

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

6、在main.js中導入并使用use掛載到app上

如下所示:

import { createApp } from 'vue'
import App from './App.vue'
import UI from "@/UI";
// 導入自己UI組件庫
// import UI from './UI'

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

這種方式的缺點是還是得在選項式上才方便使用,在組合式方式上不推薦

總結

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

相關文章

最新評論