我是靠谱客的博主 高兴小懒猪,这篇文章主要介绍【Vue】【Element】使用一个方法,打开弹窗并接收返回值使用一个方法,打开弹窗并接收返回值,现在分享给大家,希望可以做个参考。

使用一个方法,打开弹窗并接收返回值

示例

  • 父组件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<template> <div> <el-button @click="open1">打开弹窗1</el-button> <el-button @click="open2">打开弹窗2</el-button> <module-1 ref="module1" /> <module-2 ref="module2" /> </div> </template> <script> import { ElMessage } from 'element-plus' import module1 from './components/module-1.vue' import module2 from './components/module-2.vue' export default { components: { module1, module2 }, data() { return {} }, methods: { open1() { const func = value => { ElMessage(`输入的为:${value}`) } this.$refs.module1.open(func) }, open2() { this.$refs.module2.open().then(value => { ElMessage(`输入的为:${value}`) }) } } } </script>
  • 子组件(通过传递方法使用)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<template> <el-dialog title="提示" v-model="visible" width="30%"> <span> <el-input v-model="value" placeholder="请输入内容" /> </span> <template #footer> <span class="dialog-footer"> <el-button @click="visible = false">取 消</el-button> <el-button type="primary" @click="submit">确 定</el-button> </span> </template> </el-dialog> </template> <script> export default { data() { return { visible: false, value: '', callBack: null } }, methods: { // 打开弹窗 open(cb) { this.callBack = cb this.visible = true }, // 点击确认 submit() { if (this.callBack) { this.visible = false this.callBack(this.value) } } } } </script>
  • 子组件(通过Promise链式调用)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<template> <el-dialog title="提示" v-model="visible" width="30%"> <span> <el-input v-model="value" placeholder="请输入内容" /> </span> <template #footer> <span class="dialog-footer"> <el-button @click="visible = false">取 消</el-button> <el-button type="primary" @click="submit">确 定</el-button> </span> </template> </el-dialog> </template> <script> export default { data() { return { visible: false, value: '', callBack: null } }, methods: { // 打开弹窗 open() { return new Promise(cb => { this.callBack = cb this.visible = true }) }, // 点击确认 submit() { if (this.callBack) { this.visible = false this.callBack(this.value) } } } } </script>

最后

以上就是高兴小懒猪最近收集整理的关于【Vue】【Element】使用一个方法,打开弹窗并接收返回值使用一个方法,打开弹窗并接收返回值的全部内容,更多相关【Vue】【Element】使用一个方法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部