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

vue3動態(tài)加載對話框的方法實例

 更新時間:2022年03月29日 15:29:56   作者:程序員德子  
對話框是很常用的組件,在很多地方都會用到,下面這篇文章主要給大家介紹了關(guān)于vue3動態(tài)加載對話框的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

簡介

介紹使用vue3的異步組件動態(tài)管理對話框組件,簡化對話框組件使用方式。本文使用的是vue3、typescript、element_plus完成的示例。

常規(guī)方式使用對話框

一般情況下,使用對話框組件,會使用v-model進(jìn)行雙向綁定,通過visible變量控制對話框的顯示和關(guān)閉。常規(guī)方式有一個弊端,自定義組件中使用<el-dialog>,需要通過父組件控制自定義組件是否展示。

<template>
  <ElButton type="primary" @click="openGeneral()">常規(guī)方式打開Modal</ElButton>
  <el-dialog
             v-model="dialogVisible"
             title="Tips"
             width="30%"
             :before-close="handleClose"
             >
    <span>This is a message</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogVisible = false"
                   >Confirm</el-button
          >
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup

import {ref } from 'vue';
const dialogVisible = ref(false)

const openGeneral=()=>{
  dialogVisible.value=true
}

</script>

異步動態(tài)加載

先看使用異步組件進(jìn)行動態(tài)加載對話框的方式。異步組件使用到的是defineAsyncComponent接口,只有使用到這個組件,才會從網(wǎng)絡(luò)上加載。動態(tài)操作使用的useDzModal

使用方式

<template>
  <ElButton type="primary" @click="openTestModalAsync()">動態(tài)異步打開TestModal</ElButton>
</template>
<script setup lang="ts">
import { defineAsyncComponent,ref } from 'vue';  
import { ElMessageBox } from 'element-plus';
import { useDzModal } from './dzmodal'

// 異步加載組件
const TestModalAsync = defineAsyncComponent(()=>import('./components/TestModal.vue'))

const dzmodal = useDzModal()

// # 通過dzmodal動態(tài)操作對話框
const openTestModalAsync=()=>{
  dzmodal.open(TestModalAsync,{
    name:'張三'
  })
    .then(res=>{
    if(res.type==='ok'){
      ElMessageBox.alert('TestModal點擊了確定');
    }else{
      ElMessageBox.alert('TestModal點擊了取消');
    }
  })
}

</script>

TestModal.vue

<script setup lang="ts">
  import { reactive, ref, defineProps } from 'vue'
const emits = defineEmits(['ok','cancel'])
const props = defineProps({
  name: String
});
const dialogVisible = ref(true)

const resultData= reactive({
  type:'ok',
  data:{}
})

</script>

<template>
      <el-dialog
v-model="dialogVisible"
title="TestModal"
width="30%"
>
  <div>通過DzModal打開TestModal</div>
<div>外部傳入:{{ name }}</div>
<template #footer>
  <span class="dialog-footer">
    <el-button @click="emits('cancel',{});dialogVisible = false">Cancel</el-button>
<el-button type="primary" @click="emits('ok',{});dialogVisible = false"
>Confirm</el-button
>
  </span>
</template>
</el-dialog>
</template>

<style scoped>
  </style>

使用結(jié)果

動態(tài)操作對話框的實現(xiàn)

動態(tài)操作對話框,主要思路是動態(tài)創(chuàng)建虛擬dom節(jié)點,將對話框組件渲染到組件上,核心關(guān)鍵點是要動態(tài)創(chuàng)建的節(jié)點的上下文為當(dāng)前app上下文vm.appContext = this._app._context

DzModalService.ts

import { App, inject, Plugin, h, render} from 'vue'
import { ComponentOptions } from 'vue';
export const DzModalSymbol =  Symbol()
export class DzModalResult{
  type: 'ok'|'cancel'|'string' = 'ok'
  body?:any= undefined
}

export class DzModalService{
    
  private _app?:App=undefined
  constructor(app:App){
      this._app=app;
  }
  public open(modal:ComponentOptions, props?: any):Promise<DzModalResult>{

    return new Promise((reslove,reject)=>{
      if(!this._app){
        reject('_app is undefined')
        return;
      }
      
      const container = document.createElement("div");
      document.body.appendChild(container)
      
      // 這里需要合并props,傳入到組件modal
      const vm = h(modal, {
        ...props,
        onOk:(data?:any)=>{
          // 彈出框關(guān)閉時移除節(jié)點
          document.body.removeChild(container)
          reslove(this.ok(data));
        },
        onCancel:(data?:any)=>{
          reslove(this.cancel(data));
        }
      });
      // 這里很重要,關(guān)聯(lián)app上下文
      vm.appContext = this._app._context
      render(vm,container);
    });
  }

    
  public ok(data?:any):DzModalResult{
    const result = new DzModalResult();
    result.type='ok';
    result.body=data;
    return result;
  }

  public cancel(data?:any):DzModalResult{
    const result = new DzModalResult();
    result.type='cancel';
    result.body=data;
    return result;
  }
}

export function useDzModal(): DzModalService {
    const dzModal = inject<DzModalService>(DzModalSymbol)
    if(!dzModal){
        throw new Error('No DzModal provided!')
    }
    return dzModal;
}

const plugin: Plugin = {
    install(app:App, options?:{[key:string]:any}){
        const dzModal = new DzModalService(app)
        app.config.globalProperties.$dzModal= dzModal
        app.provide(DzModalSymbol, dzModal)
    }
}

export default plugin;

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import DzModal from './dzmodal'
createApp(App)
.use(ElementPlus)
.use(DzModal) // 安裝 dzmodal插件
.mount('#app')

總結(jié)

使用異步動態(tài)加載對話框,父組件無需控制對話框組件的visible屬性 , 這樣可以簡化父組件操作,不在關(guān)心對話框組件在什么時間關(guān)閉,如果對話框組件需要訪問網(wǎng)絡(luò),也在子組件中完成。父組件主要做兩件事:

  • 通過異步組件方式引入對話框組件
  • 打開對話框組件

到此這篇關(guān)于vue3動態(tài)加載對話框的文章就介紹到這了,更多相關(guān)vue3動態(tài)加載對話框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue?3?中使用?vue-router?進(jìn)行導(dǎo)航與監(jiān)聽路由變化的操作

    Vue?3?中使用?vue-router?進(jìn)行導(dǎo)航與監(jiān)聽路由變化的操作

    在Vue3中,通過useRouter和useRoute可以方便地實現(xiàn)頁面導(dǎo)航和路由變化監(jiān)聽,useRouter允許進(jìn)行頁面跳轉(zhuǎn),而useRoute結(jié)合watch可以根據(jù)路由變化更新組件狀態(tài),這些功能為Vue3應(yīng)用增加了靈活性和響應(yīng)性,使得路由管理更加高效
    2024-09-09
  • 用了babel還需要polyfill嗎原理解析

    用了babel還需要polyfill嗎原理解析

    這篇文章主要為大家介紹了用了babel是否還需要polyfill的原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Vue的v-if和v-show的區(qū)別圖文介紹

    Vue的v-if和v-show的區(qū)別圖文介紹

    這篇文章主要介紹了Vue的v-if和v-show的區(qū)別圖文介紹,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • 關(guān)閉eslint檢查和ts檢查的簡單步驟記錄

    關(guān)閉eslint檢查和ts檢查的簡單步驟記錄

    這篇文章主要給大家介紹了關(guān)于關(guān)閉eslint檢查和ts檢查的相關(guān)資料,eslint是一個JavaScript的校驗插件,通常用來校驗語法或代碼的書寫風(fēng)格,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • 前端axios取消請求總結(jié)詳解

    前端axios取消請求總結(jié)詳解

    這篇文章主要為大家介紹了前端axios取消請求總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • LogicFlow插件使用前準(zhǔn)備詳解

    LogicFlow插件使用前準(zhǔn)備詳解

    這篇文章主要為大家介紹了LogicFlow插件使用前準(zhǔn)備詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 淺談vue,angular,react數(shù)據(jù)雙向綁定原理分析

    淺談vue,angular,react數(shù)據(jù)雙向綁定原理分析

    本篇文章主要介紹了淺談vue,angular,react數(shù)據(jù)雙向綁定原理分析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue-cli腳手架初始化項目各個文件夾用途

    vue-cli腳手架初始化項目各個文件夾用途

    這篇文章主要介紹了vue-cli腳手架初始化項目各個文件夾用途,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • 使用@tap.stop阻止事件繼續(xù)傳播

    使用@tap.stop阻止事件繼續(xù)傳播

    這篇文章主要介紹了使用@tap.stop阻止事件繼續(xù)傳播,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue中關(guān)于@media媒體查詢的使用

    vue中關(guān)于@media媒體查詢的使用

    這篇文章主要介紹了vue中關(guān)于@media媒體查詢的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評論

缙云县| 赞皇县| 农安县| 农安县| 新闻| 昭通市| 田林县| 开鲁县| 兴山县| 育儿| 建德市| 屏南县| 香格里拉县| 康马县| 西峡县| 临高县| 华阴市| 太和县| 临湘市| 即墨市| 城口县| 绵竹市| 益阳市| 邓州市| 绥芬河市| 鱼台县| 吴堡县| 内丘县| 连云港市| 衢州市| 呼图壁县| 孟津县| 沈阳市| 曲周县| 汕尾市| 绍兴县| 平湖市| 洪湖市| 博野县| 即墨市| 美姑县|