Skip to content

Commit b9ff4ea

Browse files
committed
fix: ssr error
1 parent 9d469fb commit b9ff4ea

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

components/_util/Portal.tsx

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import PropTypes from './vue-types';
2-
import { defineComponent, nextTick, onBeforeUnmount, onUpdated, Teleport } from 'vue';
2+
import {
3+
defineComponent,
4+
nextTick,
5+
onBeforeMount,
6+
onBeforeUnmount,
7+
onUpdated,
8+
Teleport,
9+
} from 'vue';
310

411
export default defineComponent({
512
name: 'Portal',
@@ -9,9 +16,13 @@ export default defineComponent({
916
didUpdate: PropTypes.func,
1017
},
1118
setup(props, { slots }) {
19+
let isSSR = true;
1220
// getContainer 不会改变,不用响应式
13-
const container = props.getContainer();
14-
21+
let container: HTMLElement;
22+
onBeforeMount(() => {
23+
isSSR = false;
24+
container = props.getContainer();
25+
});
1526
onUpdated(() => {
1627
nextTick(() => {
1728
props.didUpdate?.(props);
@@ -23,6 +34,9 @@ export default defineComponent({
2334
}
2435
});
2536
return () => {
37+
if (isSSR) {
38+
return slots.default?.();
39+
}
2640
return container ? <Teleport to={container} v-slots={slots}></Teleport> : null;
2741
};
2842
},

0 commit comments

Comments
 (0)