Skip to content

Instantly share code, notes, and snippets.

@akrisiun
Last active February 21, 2021 17:56
Show Gist options
  • Save akrisiun/5629c24fbd54b88ce11a5c94244da797 to your computer and use it in GitHub Desktop.
Save akrisiun/5629c24fbd54b88ce11a5c94244da797 to your computer and use it in GitHub Desktop.
vue3-tsx

Vue tsx

https://morioh.com/p/0954c6b3b2a4
Source Code: https://github.com/hyperMoss/vue-tsx

https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html
https://github.com/vuejs/jsx-next

Vue3 lifecycle:

使用 - use

beforeCreate -> 使用 setup()
created -> 使用 setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
errorCaptured -> onErrorCaptured

 onBeforeMount!!!
 onMounted!!!

Vue3 createApp source code

const createApp = ((...args) => {
    const app = ensureRenderer().createApp(...args);
    if ((process.env.NODE_ENV !== 'production')) {
        injectNativeTagCheck(app);
    }
    const { mount } = app;
    app.mount = (containerOrSelector) => {
        const container = normalizeContainer(containerOrSelector);
        if (!container)
            return;
        const component = app._component;
        if (!isFunction(component) && !component.render && !component.template) {
            component.template = container.innerHTML;
        }
        // clear content before mounting
        container.innerHTML = '';
        const proxy = mount(container);
        if (container instanceof Element) {
            container.removeAttribute('v-cloak');
            container.setAttribute('data-v-app', '');
        }
        return proxy;
    };
    return app;
});

// Actual implementation
function h(type, propsOrChildren, children) {
    const l = arguments.length;
    if (l === 2) {
        if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
            // single vnode without props
            if (isVNode(propsOrChildren)) {
                return createVNode(type, null, [propsOrChildren]);
            }
            // props without children
            return createVNode(type, propsOrChildren);
        }
        else {
            // omit props
            return createVNode(type, null, propsOrChildren);
        }
    }
    else {
        if (l > 3) {
            children = Array.prototype.slice.call(arguments, 2);
        }
        else if (l === 3 && isVNode(children)) {
            children = [children];
        }
        return createVNode(type, propsOrChildren, children);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment