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

vue3+element-plus?Dialog對(duì)話(huà)框的使用與setup?寫(xiě)法的用法

 更新時(shí)間:2023年04月23日 10:49:57   作者:前端-阿輝  
這篇文章主要介紹了vue3+element-plus?Dialog對(duì)話(huà)框的使用?與?setup?寫(xiě)法的使用,本文通過(guò)兩種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一. 傳統(tǒng)寫(xiě)法不使用setup語(yǔ)法糖

方式一:通過(guò)v-model的方式實(shí)現(xiàn)子組件的顯示與隱藏

父組件的內(nèi)容

<template>
  <div>
    <el-button @click="open">打開(kāi)</el-button>
    <Child v-model:visible="flag" ></Child>
  </div>
</template>

<script>
  import { ref, watch } from 'vue'
  import Child from "../components/Child.vue"

  export default {
    components: {
      Child
    },

    setup() {
      const flag = ref(false)

      const open = () => {
        flag.value = true
      }
      
      watch(() => flag.value , (val) => {
        console.log("監(jiān)聽(tīng)flag值得變化:", val)
      })

      return {
        flag,
        open
      }
    }
  }
</script>

子組件內(nèi)容

<template>
  <div class="hello">
    <el-dialog title="提示" v-model="dialogVisble" width="30%" :before-close="close">
      <span>這是一段信息</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="confirm">確 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { ref, watch } from 'vue'

  export default {
    props: {
      visible: {
        type: Boolean,
        default: false
      }
    },

    setup(props, ctx) {

      const dialogVisble = ref(false)

      const close = () => {
        ctx.emit("update:visible", false);
      };

      const confirm = () => {
        console.log('你點(diǎn)擊了確定按鈕')
        ctx.emit("update:visible", false);
      }

      watch(() => props.visible, (val) => {
        console.log(props.visible, val);
        dialogVisble.value = val
      });

      return {
        dialogVisble,
        confirm,
        close
      }
    }
  }
</script>

方式二:通過(guò)為元素綁定ref的方式實(shí)現(xiàn)子組件的顯示與隱藏

父組件的內(nèi)容

<template>
  <div>
    <el-button @click="open">打開(kāi)</el-button>
    <Child ref="visibleDialog"></Child>
  </div>
</template>

<script>
  import { ref } from 'vue'
  import Child from "../components/Child.vue"

  export default {
    components: {
      Child
    },

    setup() {
      const visibleDialog = ref(null)

      const open = () => {
        visibleDialog.value.dialogVisble = true
      }

      return {
        visibleDialog,
        open
      }
    }
  }
</script>

子組件內(nèi)容

<template>
  <div class="hello">
    <el-dialog title="提示" v-model="dialogVisble" width="30%" :before-close="close">
      <span>這是一段信息</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="confirm">確 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { ref } from 'vue'

  export default {

    setup(props, ctx) {

      const dialogVisble = ref(false)

      const confirm = () => {
        console.log('你點(diǎn)擊了確定按鈕')
        dialogVisble.value = false
      }

      const close = () => {
        dialogVisble.value = false
      }

      return {
        dialogVisble,
        confirm,
        close
      }
    }
  }
</script>

2. setup語(yǔ)法糖寫(xiě)法 父組件

<template>
  <Child :user="user" ref="visiableDialog"></Child>
  <el-button type="primary" @click="openDialog">打開(kāi)彈窗</el-button>
</template>

<script setup>
import { reactive, ref } from 'vue'
import Child from "../components/childComponents.vue"

const visiableDialog = ref(null)

const user = reactive({
  name: '張三',
  age: 20
})

function openDialog() {
  visiableDialog.value.dialogVisble = true
  console.log(visiableDialog.value.dialogVisble);
}
</script>

子組件

<template>
  <div class="hello">{{ `${props.user.name}在學(xué)習(xí)VUE3` }}</div>
  <el-dialog title="提示" v-model="dialogVisble" width="30%">
    <span>這是一段信息</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm">確 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessageBox } from 'element-plus'

// 定義控制彈窗顯隱的變量
const dialogVisble = ref(false)

// 接受父組件傳過(guò)來(lái)的值
// const props = defineProps({
//   user: {
//     type: Object,
//     default: {}
//   }
// })
// 或者
const props = defineProps(['user'])

function confirm() {
  ElMessageBox.confirm('確定關(guān)閉嗎?').then(() => {
    console.log('你點(diǎn)擊了確定按鈕')
    dialogVisble.value = false
  }).catch(() => { })
}

function close() {
  dialogVisble.value = false
}

// 將變量暴露出來(lái)
defineExpose({
  dialogVisble
})
</script>

總結(jié):

  • 對(duì)于傳統(tǒng)寫(xiě)法兩種方式來(lái)看,都有各自的優(yōu)缺點(diǎn),方式一在寫(xiě)法上雖然麻煩了些,但是符合vue的設(shè)計(jì)原則,盡量少的操作Dom,以操作數(shù)據(jù)的方式達(dá)到了預(yù)期的目的。
  • 而方式二看起來(lái)趨向于我們?cè)趘ue2中的寫(xiě)法,雖然在寫(xiě)法上簡(jiǎn)便,但是在原理上則是操作了Dom,總之,兩種方式都可以達(dá)到我們想要的結(jié)果,至于使用那種方式看個(gè)人編寫(xiě)代碼的習(xí)慣吧。
  • 對(duì)于使用setup語(yǔ)法糖寫(xiě)法來(lái)看,代碼整體比較整潔,寫(xiě)起來(lái)也相對(duì)方便快捷

到此這篇關(guān)于vue3+element-plus Dialog對(duì)話(huà)框的使用與setup 寫(xiě)法的用法的文章就介紹到這了,更多相關(guān)vue3 element-plus Dialog對(duì)話(huà)框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用

    功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用

    這篇文章主要為大家介紹了功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 在Vue中使用Echarts+封裝

    在Vue中使用Echarts+封裝

    這篇文章主要介紹了在Vue中使用Echarts++封裝,需要的朋友可以參考下
    2023-11-11
  • 淺談VUE uni-app 模板語(yǔ)法

    淺談VUE uni-app 模板語(yǔ)法

    這篇文章主要介紹了uni-app 的模板語(yǔ)法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Vue注冊(cè)組件命名時(shí)不能用大寫(xiě)的原因淺析

    Vue注冊(cè)組件命名時(shí)不能用大寫(xiě)的原因淺析

    這段時(shí)間一直在弄vue,當(dāng)然也遇到很多問(wèn)題,這里就來(lái)跟大家分享一些注冊(cè)自定義模板組件的心得 ,需要的朋友可以參考下
    2019-04-04
  • vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例

    vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例

    今天小編就為大家分享一篇vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 解決vue同一slot在組件中渲染多次的問(wèn)題

    解決vue同一slot在組件中渲染多次的問(wèn)題

    今天小編就為大家分享一篇解決vue同一slot在組件中渲染多次的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Vue中的this.$emit()方法詳解

    Vue中的this.$emit()方法詳解

    這篇文章主要給大家介紹了關(guān)于Vue中this.$emit()方法的相關(guān)資料,this.$emit()是 Vue.js 中一個(gè)很有用的方法,可以幫助子組件向父組件傳遞事件,需要的朋友可以參考下
    2023-09-09
  • vue項(xiàng)目中?jsconfig.json概念及使用步驟

    vue項(xiàng)目中?jsconfig.json概念及使用步驟

    這篇文章主要介紹了vue項(xiàng)目中?jsconfig.json是什么,本文僅僅簡(jiǎn)單介紹了?jsconfig?.json?的一些基本配置,而?jsconfig?.json提供了大量能使我們快速便捷提高代碼效率的方法,需要的朋友可以參考下
    2022-07-07
  • 一文詳解vue生命周期在uniapp中的用法

    一文詳解vue生命周期在uniapp中的用法

    在uniapp中,vue的生命周期的用法基本都得以保留,但是對(duì)于特殊的需求以及特殊的情況,uniapp還引入了一些特有的生命周期鉤子,本文給大家詳細(xì)介紹了vue生命周期在uniapp中的用法,感興趣的朋友可以參考下
    2024-01-01
  • element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中

    element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中

    這篇文章主要介紹了element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論

凉城县| 湖口县| 静乐县| 定结县| 关岭| 依安县| 平和县| 赣榆县| 方山县| 罗城| 五大连池市| 吴忠市| 铜山县| 鞍山市| 乌兰浩特市| 卢湾区| 津南区| 九龙城区| 尼勒克县| 务川| 罗甸县| 临潭县| 镇赉县| 东乡| 瑞丽市| 内黄县| 文昌市| 广丰县| 上饶县| 阿坝| 耒阳市| 定安县| 开化县| 崇礼县| 佛冈县| 永和县| 若尔盖县| 洪湖市| 安泽县| 从化市| 包头市|