最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue2與Vue3如何利用install自定義全局確認框組件編寫

 更新時間:2024年03月12日 16:02:39   作者:胡小末、  
這篇文章主要介紹了Vue2與Vue3如何利用install自定義全局確認框組件編寫方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

眾所周知,vue.js框架在提供豐富的插件的前提下,還提供了自定義組件的方式。

在Vue2中使用時可以直接引用Vue實例中的extends方法來生成一個新的未掛載組件,在需要的時候再掛載顯示即可。

而Vue3中取消了extends的方法,我們沒法使用這一方式來實現(xiàn)。

所以在升級到Vue3后這一組件也是閑置了比較長一段時間。

這陣子空下來查閱資料后,找到了相應(yīng)的解決方法。

Vue2中使用install方法注冊全局組件

1.新建vue-cli項目,這個作為入門內(nèi)容在此不過多贅述。

2.在根目錄新建plugin文件夾,打開后新建message文件夾,在此文件夾中新建message.vue和index.js文件,結(jié)構(gòu)目錄如圖所示。

文件結(jié)構(gòu)(忽略box哈)

3.在box.vue中添加代碼,跟平常添加組件類似,這里我們添加的是一個通知信息的內(nèi)容。

<template>
  <transition name="slide-top">
    <section id="message" v-if="msgOueue.length > 0">
      <transition-group name="slide-top">
        <div
          class="message"
          v-for="item in msgOueue"
          :key="item.uid"
          :style="
            'color:' +
            item.config.color +
            ';background:' +
            item.config.background +
            ';border:1px solid ' +
            item.config.borderColor +
            ';'
          "
        >
          <span class="message-icons" :class="item.config.icon"></span>
          <div class="messagebox">{{ item.config.content }}</div>
        </div>
      </transition-group>
    </section>
  </transition>
</template>
<script>
export default {
  data() {
    return {
      msgOueue: [],
      config: {
        uid: "",
        content: "內(nèi)容", // 內(nèi)容
        background: "#909399",
        color: "#303133",
        borderColor: "#303133",
        icon: "el-icon-info",
      },
    };
  },
  methods: {
    // 關(guān)閉方法
    onClose() {
      this.msgOueue.shift();
      this.type = 0;
    },
  },
};
</script>
<style scoped>
#message {
  width: 60rem;
  height: 5rem;
  position: absolute;
  top: 5rem;
  left: 30rem;
}
 
.message {
  width: 56rem;
  height: 3rem;
  border-radius: 0.5rem;
  margin-bottom: 2rem;
  padding: 1rem 2rem;
  display: flex;
  justify-content: center;
  align-items: center;
}
 
.message-icons {
  display: inline-block;
  font-size: 2rem;
}
 
.messagebox {
  width: 100%;
  height: 100%;
  display: inline-block;
  margin-left: 2rem;
  font-size: 1.5rem;
  line-height: 3rem;
}
 
.slide-top-enter-active,
.slide-top-leave-active {
  will-change: transform;
  transition: all 0.8s;
}
 
.slide-top-enter {
  overflow: hidden;
  opacity: 0;
  transform: translateY(-100%);
  height: 0;
  margin-bottom: 0;
  padding: 0;
  width: 0;
}
 
.slide-top-leave-to {
  overflow: hidden;
  opacity: 0;
  transform: translateY(-100%);
  height: 0;
  margin-bottom: 0;
  padding: 0;
  width: 0;
}
</style>

4.在index.js中添加以下代碼

import MESSage from "./message";
 
var counts = 10;
 
export default {
 
  install (Vue) {
    const MESSage_EXTEND = Vue.extend(MESSage);
    const MESSage_CREATE_EL = new MESSage_EXTEND({
      el: document.createElement("div"),
    });
    document.body.appendChild(MESSage_CREATE_EL.$el);
 
    const PUBLIC_FN = {
 
      hexToRgb (hex, opacity = 1) {
        return 'rgba(' + parseInt('0x' + hex.slice(1, 3)) + ',' + parseInt('0x' + hex.slice(3, 5))
          + ',' + parseInt('0x' + hex.slice(5, 7)) + ',' + opacity + ') ';
      },
 
      success (content) {
        const UID = String(counts)
        var config = {}
 
        config.uid = UID;
        config.color = "#67C23A";
        config.background = this.hexToRgb("#e1f3d8");
        config.icon = "el-icon-success";
        config.borderColor = "#67C23A"
        config.content = content;
 
        this.show(config)
      },
 
      error (content) {
 
        var config = {}
 
        const UID = String(counts)
        config.uid = UID;
        config.color = "#F56C6C";
        config.background = this.hexToRgb("#fde2e2");
        config.icon = "el-icon-error";
        config.content = content;
        config.borderColor = "#F56C6C"
 
        this.show(config)
      },
 
      warning (content) {
        var config = {}
        const UID = String(counts)
        config.uid = UID;
        config.color = "#E6A23C";
        config.background = this.hexToRgb("#ffdaa4");
        config.icon = "el-icon-warning";
        config.content = content;
        config.borderColor = "#E6A23C"
 
        this.show(config)
      },
 
      normal (content) {
        var config = {}
        const UID = String(counts)
        config.uid = UID;
        config.color = "#303133";
        config.background = this.hexToRgb("#909399");
        config.icon = "el-icon-info";
        config.content = content;
        config.borderColor = "#303133"
 
        this.show(config)
      },
 
      self (content, color = "#303133", background = "#909399", icon = "el-icon-info", bgop = 1) {
        var config = {}
        config.show = true;
        config.color = color;
        config.background = this.hexToRgb(background, bgop);
        config.icon = icon;
        config.content = content;
        this.show(config)
      },
 
      show (config) {
        const UID = String(counts)
        MESSage_CREATE_EL.uid = UID;
 
        if (MESSage_CREATE_EL.msgOueue.length > 5) {
          MESSage_CREATE_EL.msgOueue.shift()
        }
 
        console.log(config)
 
        counts++;
        MESSage_CREATE_EL.msgOueue.push({ uid: counts, config: config });
 
        setTimeout(() => {
          MESSage_CREATE_EL.onClose();
        }, 3000)
 
 
      },
    };
 
    Vue.prototype.$message = PUBLIC_FN;
  },
};

4.在main.js中引入并使用Vue.use方法加載到全局,即可在全局調(diào)用

根據(jù)不同的調(diào)用方法顯示不同的通知欄如下:

Vue3中使用install方法注冊全局組件

前文說到Vue3取消了extends方法,意味著install無法使用Vue2這一方式來進行自定義組件掛載。

查閱資料可以知道Vue3新增了createApp方法來創(chuàng)建內(nèi)容,那么我們利用這個能否實現(xiàn)這一未掛載組件的功能呢?

1.按上一個步驟新建文件,message.vue的內(nèi)容也不需要進行改變。

2.將index.js的代碼改為以下代碼:

import { createApp } from "vue";
import MESSage from "./message";
 
var counts = 10;
 
export default {
 
    install (app) {
        // 1.實例化并綁定組件
        const MESSage_EXTEND = createApp(MESSage);
        const MESSage_CREATE_EL = MESSage_EXTEND.mount(
            document.createElement("div"),
        );
 
 
        document.body.appendChild(MESSage_CREATE_EL.$el);
 
 
        // 3.調(diào)用顯示的方法
        const PUBLIC_FN = {
 
            hexToRgb (hex, opacity = 1) {
                return 'rgba(' + parseInt('0x' + hex.slice(1, 3)) + ',' + parseInt('0x' + hex.slice(3, 5))
                    + ',' + parseInt('0x' + hex.slice(5, 7)) + ',' + opacity + ') ';
            },
 
            success (content) {
                const UID = String(counts)
                var config = {}
 
                config.uid = UID;
                config.color = "#67C23A";
                config.background = this.hexToRgb("#e1f3d8");
                config.icon = "el-icon-success";
                config.borderColor = "#67C23A"
                config.content = content;
 
                this.show(config)
            },
 
            error (content) {
 
                var config = {}
 
                const UID = String(counts)
                config.uid = UID;
                config.color = "#F56C6C";
                config.background = this.hexToRgb("#fde2e2");
                config.icon = "el-icon-error";
                config.content = content;
                config.borderColor = "#F56C6C"
 
                this.show(config)
            },
 
            warning (content) {
                var config = {}
                const UID = String(counts)
                config.uid = UID;
                config.color = "#E6A23C";
                config.background = this.hexToRgb("#ffdaa4");
                config.icon = "el-icon-warning";
                config.content = content;
                config.borderColor = "#E6A23C"
 
                this.show(config)
            },
 
            normal (content) {
                var config = {}
                const UID = String(counts)
                config.uid = UID;
                config.color = "#303133";
                config.background = this.hexToRgb("#909399");
                config.icon = "el-icon-info";
                config.content = content;
                config.borderColor = "#303133"
 
                this.show(config)
            },
 
            self (content, color = "#303133", background = "#909399", icon = "el-icon-info", bgop = 1) {
                var config = {}
                config.show = true;
                config.color = color;
                config.background = this.hexToRgb(background, bgop);
                config.icon = icon;
                config.content = content;
                this.show(config)
            },
 
            show (config) {
                const UID = String(counts)
                MESSage_CREATE_EL.uid = UID;
 
                if (MESSage_CREATE_EL.msgOueue.length > 5) {
                    MESSage_CREATE_EL.msgOueue.shift()
                }
 
                console.log(config)
 
                counts++;
                MESSage_CREATE_EL.msgOueue.push({ uid: counts, config: config });
 
                setTimeout(() => {
                    MESSage_CREATE_EL.onClose();
                }, 3000)
 
 
            },
        };
 
        // 4.掛載全局
        app.config.globalProperties.$message = PUBLIC_FN;
    },
};

在main.js中使用use方法引入全局使用

import { createApp } from 'vue'
import App from './App.vue'
 
import message from './utils/plugin/message';
 
const app = createApp(App);
 
app.use(message)
 
app.mount("#app");

在測試案例中使用以下代碼測試

this.$message.success("成功");

是的沒錯,我們在模仿element-ui中的消息通知嘿嘿

總結(jié)

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

相關(guān)文章

  • vue使用echarts實現(xiàn)折線圖

    vue使用echarts實現(xiàn)折線圖

    這篇文章主要為大家詳細介紹了vue使用echarts實現(xiàn)折線圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 3分鐘搞定vite項目(vue/react)使用vite-plugin-pwa配置為pwa應(yīng)用

    3分鐘搞定vite項目(vue/react)使用vite-plugin-pwa配置為pwa應(yīng)用

    這篇文章主要介紹了3分鐘搞定vite項目(vue/react)使用vite-plugin-pwa配置為pwa應(yīng)用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-02-02
  • 解決vue-cli單頁面手機應(yīng)用input點擊手機端虛擬鍵盤彈出蓋住input問題

    解決vue-cli單頁面手機應(yīng)用input點擊手機端虛擬鍵盤彈出蓋住input問題

    今天小編就為大家分享一篇解決vue-cli單頁面手機應(yīng)用input點擊手機端虛擬鍵盤彈出蓋住input問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Swiper在Vue2中的簡單使用方法

    Swiper在Vue2中的簡單使用方法

    這篇文章主要給大家介紹了關(guān)于Swiper在Vue2中的簡單使用方法,swiper是一款現(xiàn)代化的移動端輪播組件,可以在Vue中輕松使用,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • vue路由切換時取消之前的所有請求操作

    vue路由切換時取消之前的所有請求操作

    這篇文章主要介紹了vue路由切換時取消之前的所有請求操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 在vue項目中使用Jquery-contextmenu插件的步驟講解

    在vue項目中使用Jquery-contextmenu插件的步驟講解

    今天小編就為大家分享一篇關(guān)于在vue項目中使用Jquery-contextmenu插件的步驟講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法(推薦)

    Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法(推薦)

    vue router給我們提供了兩種頁面間傳遞參數(shù)的方式,一種是動態(tài)路由匹配,一種是編程式導(dǎo)航,接下來通過本文給大家介紹Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法,需要的朋友可以參考下
    2018-11-11
  • vue實現(xiàn)圖片上傳預(yù)覽功能

    vue實現(xiàn)圖片上傳預(yù)覽功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)圖片上傳預(yù)覽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • vue iview實現(xiàn)分頁功能

    vue iview實現(xiàn)分頁功能

    這篇文章主要為大家詳細介紹了vue iview實現(xiàn)分頁功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • vue項目使用.env文件配置全局環(huán)境變量的方法

    vue項目使用.env文件配置全局環(huán)境變量的方法

    這篇文章主要介紹了vue項目使用.env文件配置全局環(huán)境變量的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10

最新評論

栾川县| 凤阳县| 九台市| 赤水市| 麻城市| 石门县| 淮阳县| 金寨县| 调兵山市| 淮滨县| 都昌县| 襄城县| 从江县| 富阳市| 年辖:市辖区| 湖北省| 苏尼特左旗| 固阳县| 威信县| 景德镇市| 柳河县| 丽江市| 云林县| 柏乡县| 望谟县| 准格尔旗| 东丰县| 来安县| 苍山县| 佛山市| 商洛市| 佛学| 扶风县| 安庆市| 兰溪市| 松溪县| 长宁区| 娱乐| 秭归县| 闸北区| 曲松县|