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

Vue消息提示this.$message方法使用解讀

 更新時間:2023年09月26日 08:36:22   作者:老李四  
這篇文章主要介紹了Vue消息提示this.$message方法使用,具有很好的參考價值,希望對大家有所幫助,

Vue消息提示this.$message方法

HTML

<el-button @click="saveData">彈窗</el-button>

JavaScript

saveData(){
? ? this.$message({undefined
? ? ? ? message:“這是彈框消息”,
? ? ? ? type:‘success'
? ? ?})
?// type 取值 ‘success'(成功) /warning(警告)/info(消息)/error(錯誤)/
}

element 的 this.$message( ) 消息提示實現

在vue項目中,直接通過js代碼 this.$message( )就可以調出消息提示組件,這是如何實現的呢

主要分為以下幾步

1.用 Vue.extend 創(chuàng)建組件的模板(構造函數)

2.創(chuàng)建一個函數,在函數內部, 實例化組件并進行掛載到相應元素上

3.將創(chuàng)建的這個函數綁定到Vue原型上,就可以通過this .訪問了

上代碼,

如下目錄結構

在main.js中

import Vue from "vue";
import message from "./main.vue";
// 1.用 Vue.extend 創(chuàng)建組件的模板(構造函數)
let messageConstructor = Vue.extend(message);
let instance;
const Message = function(options) {// options是傳入的參數配置 {message: '成功',type: "success"offset: 80}
  // 2.實例化組件
  instance = new messageConstructor({ data: options }); // 把options導入data中
  // 3.組件掛載
  instance.$mount();
  document.body.appendChild(instance.$el);
  // 設置offset
  let verticalOffset = options.offset || 20;
  instance.verticalOffset = verticalOffset;
  instance.duration = options.duration || 3000;
  return instance;
};
export default Message;

在main.vue中

 
<template>
  <div  v-show="visible"
    :class="['my-message', 'my-message-' + type, center ? 'is-center' : '']"
    :style="positionStyle"
  >
    <slot>
      <p>{{ message }}</p>
    </slot>
    <i v-if="showClose" class="el-icon-close" @click="close"></i>
  </div>
</template>
<script>
export default {
  name: "Message",
  data() {
    return {
      visible:false,
      verticalOffset: 20,
      duration: 3000,
      timer: null,
      center: false,
      showClose: false,
       closed: false,
    };
  },
  mounted() {
    this.autoClose();
  },
  beforeDestroy() {
    clearTimeout(this.timer);
  },
    watch: {
      closed(newVal) {
        if (newVal) {
          this.visible = false;
        }
      }
    },
  computed: {
    positionStyle() {
      return {
        top: `${this.verticalOffset}px`,
      };
    },
  },
  methods: {
    autoClose() {
      this.timer = setTimeout(() => {
        this.$destroy(true);
        this.$el.parentNode.removeChild(this.$el);
      }, this.duration);
    },
    close() {
      this.closed = true;
      clearTimeout(this.timer);
    }
  },
};
</script>
<style>
.my-message {
  min-width: 380px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  position: fixed;
  left: 50%;
  top: 20px;
  transform: translateX(-50%);
  background-color: #edf2fc;
  transition: opacity 0.3s, transform 0.4s, top 0.4s;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding: 0 15px;
}
p {
}
.my-message-info {
  color: #909399;
}
.my-message-success {
  background: #f2f9ec;
  color: #67c23a;
  border-color: #e4f2da;
}
.my-message-warning {
  background: #fcf6ed;
  color: #e6a23c;
  border-color: #f8ecda;
}
.my-message-error {
  background: #fcf0f0;
  color: #f56c6c;
  border-color: #f9e3e2;
}
.is-center {
  justify-content: center;
}
</style>

在index.js中

import Vue from "vue";
import Message from './src/main'
Vue.prototype.$message = Message 

ok了,大晚上的有點困,有些地方有些潦草,有時間再改下。。。

總結

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

相關文章

  • Vue3超詳細的ref()用法教程(看這一篇就夠了!)

    Vue3超詳細的ref()用法教程(看這一篇就夠了!)

    這篇文章主要給大家介紹了關于Vue3超詳細的ref()用法的相關資料,在Vue3中ref函數不僅可以用于在組件中獲取DOM元素或子組件的引用,還可以直接訪問組件元素本身,需要的朋友可以參考下
    2023-07-07
  • vue3 element-plus二次封裝組件系列之伸縮菜單制作

    vue3 element-plus二次封裝組件系列之伸縮菜單制作

    這篇文章主要介紹了vue3 element-plus二次封裝組件系列之伸縮菜單制作,是基于vue3 vite element-plus搭建的,值的注意的時候,里面的圖標組件是經過處理的,結合實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • vue中rem的配置的方法示例

    vue中rem的配置的方法示例

    這篇文章主要介紹了vue中rem的配置的方法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • VUE2舊項目重新安裝依賴后@vue/compiler-sfc編譯報錯問題

    VUE2舊項目重新安裝依賴后@vue/compiler-sfc編譯報錯問題

    在VUE2舊項目中重新安裝依賴后,如果遇到@vue/compiler-sfc編譯報錯,首先需要檢查package.json文件中的Vue版本是否升級到2.7版本,2.7版本的編譯插件不再支持/deep/這種樣式穿透,版本^和~的區(qū)別在于^只能鎖住第一位數
    2025-01-01
  • vue編寫簡單的購物車功能

    vue編寫簡單的購物車功能

    這篇文章主要為大家詳細介紹了vue編寫簡單的購物車功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • vue3清空reactive的四種方式

    vue3清空reactive的四種方式

    本文主要介紹了vue3清空reactive的四種方式,包含使用?Object.assign,使用?Object.keys?和?for...in?循環(huán),使用?delete?操作符和重新賦值4種,感興趣的可以了解一下
    2024-03-03
  • Vue3中其他的Composition?API詳解

    Vue3中其他的Composition?API詳解

    這篇文章主要介紹了Vue3中其他的Composition?API,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • 基于Vue3文件拖拽上傳功能實現

    基于Vue3文件拖拽上傳功能實現

    這篇文章主要介紹了Vue3文件拖拽上傳功能,支持拖拽到此區(qū)域上傳,支持選擇多個文件/文件夾,代碼很簡單,對vue3拖拽上傳功能感興趣的朋友一起看看吧
    2022-10-10
  • vue組件實現文字居中對齊的方法

    vue組件實現文字居中對齊的方法

    這篇文章主要為大家詳細介紹了vue組件實現文字居中對齊的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 基于vue+echarts 數據可視化大屏展示的方法示例

    基于vue+echarts 數據可視化大屏展示的方法示例

    這篇文章主要介紹了基于vue+echarts 數據可視化大屏展示的方法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2020-03-03

最新評論