fix: ssr error

pull/5026/head
tangjinzhou 2021-12-12 16:47:30 +08:00
parent 9d469fb2af
commit b9ff4eabcc
1 changed files with 17 additions and 3 deletions

View File

@ -1,5 +1,12 @@
import PropTypes from './vue-types';
import { defineComponent, nextTick, onBeforeUnmount, onUpdated, Teleport } from 'vue';
import {
defineComponent,
nextTick,
onBeforeMount,
onBeforeUnmount,
onUpdated,
Teleport,
} from 'vue';
export default defineComponent({
name: 'Portal',
@ -9,9 +16,13 @@ export default defineComponent({
didUpdate: PropTypes.func,
},
setup(props, { slots }) {
let isSSR = true;
// getContainer
const container = props.getContainer();
let container: HTMLElement;
onBeforeMount(() => {
isSSR = false;
container = props.getContainer();
});
onUpdated(() => {
nextTick(() => {
props.didUpdate?.(props);
@ -23,6 +34,9 @@ export default defineComponent({
}
});
return () => {
if (isSSR) {
return slots.default?.();
}
return container ? <Teleport to={container} v-slots={slots}></Teleport> : null;
};
},