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

Vue3?函數(shù)式彈窗的實(shí)例小結(jié)

 更新時間:2023年11月24日 11:20:14   作者:-小龍人  
這篇文章主要介紹了Vue3?函數(shù)式彈窗的實(shí)例小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

運(yùn)行環(huán)境

  • vue3
  • vite
  • ts
  • element-plus

開發(fā)與測試

1. 使用h、render函數(shù)創(chuàng)建Dialog

  • 建議可在plugins目錄下創(chuàng)建dialog文件夾,創(chuàng)建index.ts文件,代碼如下
import { h, render } from "vue";
/**
 * 函數(shù)式彈窗
 * @param component 組件
 * @param options 組件參數(shù)
 * @returns
 */
function createDialog(component: any, options: any) {
  return new Promise((resolve, reject) => {
    // 創(chuàng)建一個div節(jié)點(diǎn)
    const mountNode = document.createElement("div");
    // 將div節(jié)點(diǎn)拼接到Dom的body節(jié)點(diǎn)下
    document.body.appendChild(mountNode);
    // 使用h函數(shù)創(chuàng)建節(jié)點(diǎn)
    const vNode = h(component, {
      ...options,
      // 注意: vue子組件emit回調(diào)事件名稱必須以on開頭
      onSubmit: data => {
        resolve(data);
        // 移除節(jié)點(diǎn)
        document.body.removeChild(mountNode);
      },
      onCancel: data => {
        reject(data);
        // 移除節(jié)點(diǎn)
        document.body.removeChild(mountNode);
      }
    });
    // 渲染Dialog
    render(vNode, mountNode);
  });
}
export default createDialog;

2. 全局掛載函數(shù)式彈窗

在main.ts中引入彈窗,并掛載在app上

// 引入函數(shù)式彈窗
import Dialog from "@/plugins/dialog";
const app = createApp(App);
// 掛載到app
app.config.globalProperties.$dialog = Dialog;

3. 測試

3.1 創(chuàng)建一個彈窗組件 testDialog.vue

<template>
  <el-dialog v-model="dialogVisible" title="測試函數(shù)式彈窗" width="50%">
    <span>{{ props.content }}</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">Cancel</el-button>
        <el-button type="primary" @click="handleSubmit"> Submit </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, toRefs } from "vue";
// 注意: 需要按需引入使用到的第三方UI組件
import { ElDialog, ElButton } from "element-plus";
const props = withDefaults(
  defineProps<{
    show?: boolean; // moadl開關(guān)
    content?: string; // 內(nèi)容
  }>(),
  {}
);
const emits = defineEmits(["submit", "cancel"]);
const state = reactive({
  dialogVisible: props.show
});
const { dialogVisible } = toRefs(state);
/** submit */
const handleSubmit = () => {
  // 回調(diào)
  emits("submit", { action: "submit", msg: "submit back" });
  // 關(guān)閉彈窗
  dialogVisible.value = false;
};
/** cancel */
const handleCancel = () => {
  // 回調(diào)
  emits("cancel", { action: "cancel", msg: "cancel back" });
  // 關(guān)閉彈窗
  dialogVisible.value = false;
};
</script>

3.2 函數(shù)式調(diào)用彈窗

<template>
  <!-- 動態(tài)函數(shù)式彈窗 -->
  <div class="test_dialog">
    <el-button @click="openModal">調(diào)用函數(shù)式彈窗</el-button>
  </div>
</template>
<script lang="ts" setup>
import { getCurrentInstance } from "vue";
import TestDialog from "./testDialog.vue";
// 通過全局的上下文拿到 proxy 屬性
const { proxy } = getCurrentInstance();
// 調(diào)用函數(shù)式彈窗
const openModal = () => {
  // 調(diào)用彈窗
  proxy
    .$dialog(TestDialog, {
      show: true,
      content: "調(diào)用彈窗成功了!"
    })
    .then(res => {
      // submit
      console.log(res);
    })
    .catch(error => {
      // cancel 回調(diào)
      console.log(error);
    });
};
</script>
<style lang="scss" scoped>
.test_dialog {
  padding: 50px;
}
</style>

3.3 測試效果

問題

非原生的html元素?zé)o法渲染,如elements-plus組件無法在彈窗渲染
因?yàn)槭褂胔函數(shù)無法渲染第三方UI,需要在彈窗中單獨(dú)引入,如上面測試代碼使用的element-plus的modal和button都需要按需引入一次。如果沒有引入彈窗都不會show出來,控制臺會給于警告如下截圖,通過這個截圖也可以看到,h函數(shù)是幫我們將彈窗組件拼接到了DOM中,組件的參數(shù)一并拼接了進(jìn)去,與傳統(tǒng)的調(diào)用方式近似。

在調(diào)用dialog的代碼中,ts會有代碼警告
可以全局申明下掛載的dialog,可直接在main.ts添加下面的申明

// 全局申明下$dialog,可以去除調(diào)用時ts的警告
	declare module "vue" {
	  export interface ComponentCustomProperties {
	    $dialog: any;
	  }
	}

到此這篇關(guān)于Vue3 函數(shù)式彈窗的文章就介紹到這了,更多相關(guān)Vue3 函數(shù)式彈窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue實(shí)現(xiàn)內(nèi)部組件輪播切換效果的示例代碼

    Vue實(shí)現(xiàn)內(nèi)部組件輪播切換效果的示例代碼

    這篇文章主要介紹了Vue實(shí)現(xiàn)內(nèi)部組件輪播切換效果的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue.js將unix時間戳轉(zhuǎn)換為自定義時間格式

    vue.js將unix時間戳轉(zhuǎn)換為自定義時間格式

    這篇文章主要介紹了vue.js將unix時間戳轉(zhuǎn)換為自定義時間格式的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 關(guān)于el-select組件設(shè)置默認(rèn)值的實(shí)現(xiàn)方式

    關(guān)于el-select組件設(shè)置默認(rèn)值的實(shí)現(xiàn)方式

    這篇文章主要介紹了關(guān)于el-select組件設(shè)置默認(rèn)值的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue-cli3跨域配置的簡單方法

    vue-cli3跨域配置的簡單方法

    這篇文章主要給大家介紹了關(guān)于vue-cli3跨域配置的簡單方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue-cli3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • vue2使用ts?vue-class-component的過程

    vue2使用ts?vue-class-component的過程

    vue-property-decorator?是一個?Vue.js?的裝飾器庫,它提供了一些裝飾器來讓你在?Vue?組件中定義屬性、計(jì)算屬性、方法、事件等,本文給大家介紹vue2使用ts?vue-class-component的相關(guān)知識,感興趣的朋友一起看看吧
    2023-11-11
  • vue中created、watch和computed的執(zhí)行順序詳解

    vue中created、watch和computed的執(zhí)行順序詳解

    由于vue的雙向數(shù)據(jù)綁定,自動更新數(shù)據(jù)的機(jī)制,在數(shù)據(jù)變化后,對此數(shù)據(jù)依賴?的所有數(shù)據(jù),watch事件都會被更新、觸發(fā),下面這篇文章主要給大家介紹了關(guān)于vue中created、watch和computed的執(zhí)行順序,需要的朋友可以參考下
    2022-11-11
  • vue3+ts中定義ref變量,設(shè)置變量類型方式

    vue3+ts中定義ref變量,設(shè)置變量類型方式

    這篇文章主要介紹了vue3+ts中定義ref變量,設(shè)置變量類型方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue-Router路由守衛(wèi)詳?shù)募?xì)用法教程

    Vue-Router路由守衛(wèi)詳?shù)募?xì)用法教程

    在Vue.js應(yīng)用中,Vue-Router是一個非常重要的插件,它允許我們實(shí)現(xiàn)頁面間的導(dǎo)航,然而,僅僅實(shí)現(xiàn)導(dǎo)航是不夠的,我們還需要在導(dǎo)航的不同階段進(jìn)行各種操作,本文將結(jié)合實(shí)際案例,詳細(xì)介紹Vue-Router路由守衛(wèi)的用法,需要的朋友可以參考下
    2024-12-12
  • 實(shí)例詳解Vue項(xiàng)目使用eslint + prettier規(guī)范代碼風(fēng)格

    實(shí)例詳解Vue項(xiàng)目使用eslint + prettier規(guī)范代碼風(fēng)格

    這篇文章主要介紹了Vue項(xiàng)目使用eslint + prettier規(guī)范代碼風(fēng)格,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-08-08
  • vue element實(shí)現(xiàn)多個Formt表單同時驗(yàn)證

    vue element實(shí)現(xiàn)多個Formt表單同時驗(yàn)證

    這篇文章主要介紹了vue element實(shí)現(xiàn)多個Formt表單同時驗(yàn)證方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論