Skip to content

Commit 9cdf297

Browse files
authored
feat: message-box support appendTarget (#65)
1 parent 1e6a140 commit 9cdf297

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

examples/docs/zh-CN/message-box.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
this.$message({
6464
type: 'info',
6565
message: '已取消删除'
66-
});
66+
});
6767
});
6868
}
6969
}
@@ -101,7 +101,7 @@
101101
this.$message({
102102
type: 'info',
103103
message: '取消输入'
104-
});
104+
});
105105
});
106106
}
107107
}
@@ -273,6 +273,35 @@
273273
```
274274
:::
275275

276+
### 实例插入位置
277+
支持实例插入文档位置
278+
279+
:::demo 将 `appendTarget` 设置为 要插入的位置 即可
280+
281+
```html
282+
<template>
283+
<div ref="target" id="target">
284+
<el-button type="text" @click="open">点击打开 Message Box</el-button>
285+
</div>
286+
</template>
287+
288+
<script>
289+
export default {
290+
methods: {
291+
open() {
292+
this.$confirm('打开控制台查看插入的位置', '提示', {
293+
confirmButtonText: '确定',
294+
cancelButtonText: '取消',
295+
center: true,
296+
appendTarget: this.$refs.target // '#target'
297+
})
298+
}
299+
}
300+
}
301+
</script>
302+
```
303+
:::
304+
276305
### 全局方法
277306

278307
如果你完整引入了 Element,它会为 Vue.prototype 添加如下全局方法:$msgbox, $alert, $confirm 和 $prompt。因此在 Vue instance 中可以采用本页面中的方式调用 `MessageBox`。调用参数为:
@@ -324,3 +353,4 @@ import { MessageBox } from 'element-ui';
324353
| inputErrorMessage | 校验未通过时的提示文本 | string || 输入的数据不合法! |
325354
| center | 是否居中布局 | boolean || false |
326355
| roundButton | 是否使用圆角按钮 | boolean || false |
356+
| appendTarget | 实例插入文档的目标位置 | HTMLElement / string || document.body |

packages/message-box/src/main.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const defaults = {
3030
dangerouslyUseHTMLString: false,
3131
center: false,
3232
roundButton: false,
33-
distinguishCancelAndClose: false
33+
distinguishCancelAndClose: false,
34+
appendTarget: null
3435
};
3536

3637
import Vue from 'vue';
@@ -43,6 +44,18 @@ const MessageBoxConstructor = Vue.extend(msgboxVue);
4344
let currentMsg, instance;
4445
let msgQueue = [];
4546

47+
const getAppendTarget = (target) => {
48+
if (!target) {
49+
return document.body;
50+
}
51+
if (typeof target === 'string') {
52+
return document.querySelector(target);
53+
}
54+
if (target instanceof HTMLElement) {
55+
return target;
56+
}
57+
};
58+
4659
const defaultCallback = action => {
4760
if (currentMsg) {
4861
let callback = currentMsg.callback;
@@ -111,7 +124,7 @@ const showNextMsg = () => {
111124
instance[prop] = true;
112125
}
113126
});
114-
document.body.appendChild(instance.$el);
127+
getAppendTarget(options.appendTarget).appendChild(instance.$el);
115128

116129
Vue.nextTick(() => {
117130
instance.visible = true;

0 commit comments

Comments
 (0)