feat: add mountComponent utils

This commit is contained in:
chenjiahan 2020-08-31 17:49:19 +08:00
parent 77613e3ee5
commit f02afd882c
5 changed files with 34 additions and 32 deletions

View File

@ -1,14 +1,11 @@
import { createApp, nextTick } from 'vue'; import { nextTick } from 'vue';
import { inBrowser, mountComponent } from '../utils';
import VanDialog from './Dialog'; import VanDialog from './Dialog';
import { inBrowser } from '../utils';
let instance; let instance;
function initInstance() { function initInstance() {
const root = document.createElement('div'); ({ instance } = mountComponent({
document.body.appendChild(root);
instance = createApp({
data() { data() {
return { return {
dialogProps: { dialogProps: {
@ -35,7 +32,7 @@ function initInstance() {
/> />
); );
}, },
}).mount(root); }));
} }
function Dialog(options) { function Dialog(options) {

View File

@ -1,6 +1,6 @@
import { createApp, nextTick } from 'vue'; import { nextTick } from 'vue';
import { inBrowser, mountComponent } from '../utils';
import VanImagePreview from './ImagePreview'; import VanImagePreview from './ImagePreview';
import { inBrowser } from '../utils';
let instance; let instance;
@ -26,10 +26,7 @@ const defaultConfig = {
}; };
function initInstance() { function initInstance() {
const root = document.createElement('div'); ({ instance } = mountComponent({
document.body.appendChild(root);
instance = createApp({
data() { data() {
return { return {
props: { props: {
@ -62,7 +59,7 @@ function initInstance() {
/> />
); );
}, },
}).mount(root); }));
} }
const ImagePreview = (images, startPosition = 0) => { const ImagePreview = (images, startPosition = 0) => {

View File

@ -1,6 +1,6 @@
import { createApp, nextTick } from 'vue'; import { nextTick } from 'vue';
import { isObject, inBrowser, mountComponent } from '../utils';
import VanNotify from './Notify'; import VanNotify from './Notify';
import { isObject, inBrowser } from '../utils';
let timer; let timer;
let instance; let instance;
@ -10,10 +10,7 @@ function parseOptions(message) {
} }
function initInstance() { function initInstance() {
const root = document.createElement('div'); ({ instance } = mountComponent({
document.body.appendChild(root);
instance = createApp({
data() { data() {
return { return {
notifyProps: { notifyProps: {
@ -39,7 +36,7 @@ function initInstance() {
/> />
); );
}, },
}).mount(root); }));
} }
function Notify(options) { function Notify(options) {

View File

@ -1,6 +1,6 @@
import { createApp, nextTick } from 'vue'; import { nextTick } from 'vue';
import { isObject, inBrowser, mountComponent } from '../utils';
import VanToast from './Toast'; import VanToast from './Toast';
import { isObject, inBrowser } from '../utils';
const defaultOptions = { const defaultOptions = {
icon: '', icon: '',
@ -52,10 +52,7 @@ function createInstance() {
queue = queue.filter((item) => isInDocument(item.$el)); queue = queue.filter((item) => isInDocument(item.$el));
if (!queue.length || multiple) { if (!queue.length || multiple) {
const root = document.createElement('div'); const { instance, unmount } = mountComponent({
document.body.appendChild(root);
const app = createApp({
data() { data() {
return { return {
timer: null, timer: null,
@ -97,8 +94,7 @@ function createInstance() {
if (multiple && inBrowser) { if (multiple && inBrowser) {
clearTimeout(this.timer); clearTimeout(this.timer);
queue = queue.filter((item) => item !== this); queue = queue.filter((item) => item !== this);
app.unmount(); unmount();
document.body.removeChild(root);
} }
}, },
}, },
@ -115,9 +111,7 @@ function createInstance() {
}, },
}); });
const toast = app.mount(root); queue.push(instance);
queue.push(toast);
} }
return queue[queue.length - 1]; return queue[queue.length - 1];

View File

@ -1,3 +1,5 @@
import { createApp, Component } from 'vue';
export { addUnit, getSizeStyle } from './format/unit'; export { addUnit, getSizeStyle } from './format/unit';
export { createNamespace } from './create'; export { createNamespace } from './create';
@ -40,3 +42,18 @@ export function pick(obj: Record<string, any>, keys: string[]) {
return ret; return ret;
}, {} as Record<string, any>); }, {} as Record<string, any>);
} }
export function mountComponent(RootComponent: Component) {
const app = createApp(RootComponent);
const root = document.createElement('div');
document.body.appendChild(root);
return {
instance: app.mount(root),
unmount() {
app.unmount(root);
document.body.removeChild(root);
},
};
}