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 { inBrowser } from '../utils';
let instance;
function initInstance() {
const root = document.createElement('div');
document.body.appendChild(root);
instance = createApp({
({ instance } = mountComponent({
data() {
return {
dialogProps: {
@ -35,7 +32,7 @@ function initInstance() {
/>
);
},
}).mount(root);
}));
}
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 { inBrowser } from '../utils';
let instance;
@ -26,10 +26,7 @@ const defaultConfig = {
};
function initInstance() {
const root = document.createElement('div');
document.body.appendChild(root);
instance = createApp({
({ instance } = mountComponent({
data() {
return {
props: {
@ -62,7 +59,7 @@ function initInstance() {
/>
);
},
}).mount(root);
}));
}
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 { isObject, inBrowser } from '../utils';
let timer;
let instance;
@ -10,10 +10,7 @@ function parseOptions(message) {
}
function initInstance() {
const root = document.createElement('div');
document.body.appendChild(root);
instance = createApp({
({ instance } = mountComponent({
data() {
return {
notifyProps: {
@ -39,7 +36,7 @@ function initInstance() {
/>
);
},
}).mount(root);
}));
}
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 { isObject, inBrowser } from '../utils';
const defaultOptions = {
icon: '',
@ -52,10 +52,7 @@ function createInstance() {
queue = queue.filter((item) => isInDocument(item.$el));
if (!queue.length || multiple) {
const root = document.createElement('div');
document.body.appendChild(root);
const app = createApp({
const { instance, unmount } = mountComponent({
data() {
return {
timer: null,
@ -97,8 +94,7 @@ function createInstance() {
if (multiple && inBrowser) {
clearTimeout(this.timer);
queue = queue.filter((item) => item !== this);
app.unmount();
document.body.removeChild(root);
unmount();
}
},
},
@ -115,9 +111,7 @@ function createInstance() {
},
});
const toast = app.mount(root);
queue.push(toast);
queue.push(instance);
}
return queue[queue.length - 1];

View File

@ -1,3 +1,5 @@
import { createApp, Component } from 'vue';
export { addUnit, getSizeStyle } from './format/unit';
export { createNamespace } from './create';
@ -40,3 +42,18 @@ export function pick(obj: Record<string, any>, keys: string[]) {
return ret;
}, {} 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);
},
};
}