fix(vue): components inside of ion-nav are now unmounted properly (#23240)

resolves #23233
This commit is contained in:
Liam DeBeasi
2021-04-26 12:58:34 -04:00
committed by GitHub
parent e436439e10
commit f2f41e2af4
3 changed files with 17 additions and 8 deletions

View File

@ -18,7 +18,7 @@ export const IonApp = defineComponent({
/** /**
* When rendering user components inside of * When rendering user components inside of
* ion-modal, ion-popover, or ion-nav, the component * ion-modal, or ion-popover the component
* needs to be created inside of the current application * needs to be created inside of the current application
* context otherwise libraries such as vue-i18n or vuex * context otherwise libraries such as vue-i18n or vuex
* will not work properly. * will not work properly.

View File

@ -1,15 +1,24 @@
import { defineComponent, h } from 'vue'; import { defineComponent, h, shallowRef, VNode } from 'vue';
import { VueDelegate } from '../framework-delegate'; import { VueDelegate } from '../framework-delegate';
export const IonNav = defineComponent({ export const IonNav = defineComponent({
name: 'IonNav', name: 'IonNav',
setup(_, { slots }) { setup() {
const delegate = VueDelegate(); const views = shallowRef([]);
/**
* Allows us to create the component
* within the Vue application context.
*/
const addView = (component: VNode) => views.value = [...views.value, component];
const removeView = (component: VNode) => views.value = views.value.filter(cmp => cmp !== component);
const delegate = VueDelegate(addView, removeView);
return () => { return () => {
return h( return h(
'ion-nav', 'ion-nav',
{ delegate }, { delegate },
slots views.value
) )
} }
} }

View File

@ -1,6 +1,6 @@
import { h, Teleport, VNode } from 'vue'; import { h, Teleport, VNode } from 'vue';
import { addTeleportedUserComponent, removeTeleportedUserComponent } from './components/IonApp'; import { addTeleportedUserComponent, removeTeleportedUserComponent } from './components/IonApp';
export const VueDelegate = () => { export const VueDelegate = (addFn = addTeleportedUserComponent, removeFn = removeTeleportedUserComponent) => {
let Component: VNode | undefined; let Component: VNode | undefined;
const attachViewToDom = (parentElement: HTMLElement, component: any, componentProps: any = {}, classes?: string[]) => { const attachViewToDom = (parentElement: HTMLElement, component: any, componentProps: any = {}, classes?: string[]) => {
/** /**
@ -22,13 +22,13 @@ export const VueDelegate = () => {
h(component, { ...componentProps }) h(component, { ...componentProps })
); );
addTeleportedUserComponent(Component); addFn(Component);
return div; return div;
} }
const removeViewFromDom = () => { const removeViewFromDom = () => {
Component && removeTeleportedUserComponent(Component); Component && removeFn(Component);
return Promise.resolve(); return Promise.resolve();
} }