vue3+element-plus Dialog对话框的使用 与 setup 写法的使用

16 篇文章 0 订阅
一. 传统写法不使用setup语法糖

方式一:通过v-model的方式实现子组件的显示与隐藏

  1. 父组件的内容
<template>
  <div>
    <el-button @click="open">打开</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("监听flag值得变化:", val)
      })

      return {
        flag,
        open
      }
    }
  }
</script>
  1. 子组件内容
<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('你点击了确定按钮')
        ctx.emit("update:visible", false);
      }

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

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

方式二:通过为元素绑定ref的方式实现子组件的显示与隐藏

  1. 父组件的内容
<template>
  <div>
    <el-button @click="open">打开</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>
  1. 子组件内容
<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('你点击了确定按钮')
        dialogVisble.value = false
      }

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

      return {
        dialogVisble,
        confirm,
        close
      }
    }
  }
</script>
2. setup语法糖写法
  1. 父组件
<template>
  <Child :user="user" ref="visiableDialog"></Child>
  <el-button type="primary" @click="openDialog">打开弹窗</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>
  1. 子组件
<template>
  <div class="hello">{{ `${props.user.name}在学习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)

// 接受父组件传过来的值
// const props = defineProps({
//   user: {
//     type: Object,
//     default: {}
//   }
// })
// 或者
const props = defineProps(['user'])

function confirm() {
  ElMessageBox.confirm('确定关闭吗?').then(() => {
    console.log('你点击了确定按钮')
    dialogVisble.value = false
  }).catch(() => { })
}

function close() {
  dialogVisble.value = false
}

// 将变量暴露出来
defineExpose({
  dialogVisble
})
</script>

总结:

  • 对于传统写法两种方式来看,都有各自的优缺点,方式一在写法上虽然麻烦了些,但是符合vue的设计原则,尽量少的操作Dom,以操作数据的方式达到了预期的目的。
  • 而方式二看起来趋向于我们在vue2中的写法,虽然在写法上简便,但是在原理上则是操作了Dom,总之,两种方式都可以达到我们想要的结果,至于使用那种方式看个人编写代码的习惯吧。
  • 对于使用setup语法糖写法来看,代码整体比较整洁,写起来也相对方便快捷
  • 16
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
以下是使用Vue 3.0+Element-Plus开发后台管理系统的步骤: 1.首先,确保你已经安装了Node.js和npm包管理器。 2.使用以下命令安装Vue CLI 4: ```shell npm install -g @vue/cli ``` 3.使用Vue CLI创建一个新项目: ```shell vue create my-project ``` 4.在创建项目时,选择使用Vue 3.0版本,并启用class-style component语法: ```shell ? Please pick a preset: Manually select features ? Check the features needed for your project: Choose Vue version, Babel, Router, Vuex, CSS Pre-processors, Linter, Unit ? Choose a version of Vue.js that you want to start the project with 3.x (Preview) ? Use class-style component syntax? Yes ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? No ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass) ? Pick a linter / formatter config: ESLint with error prevention only ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ``` 5.安装Element-Plus和Echarts 5.0: ```shell npm install element-plus [email protected] ``` 6.在main.js中引入Element-Plus和Echarts: ```javascript import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' import echarts from 'echarts' const app = createApp(App) app.use(store) app.use(router) app.use(ElementPlus) app.config.globalProperties.$echarts = echarts app.mount('#app') ``` 7.现在你可以开始使用Element-Plus和Echarts来开发你的后台管理系统了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值