fix: ssr error
parent
9d469fb2af
commit
b9ff4eabcc
|
@ -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;
|
||||
};
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue