11<template >
2- <iframe v-if =" href" id =" demo" class =" iframe demo-model" scrolling =" auto" frameborder =" 0" :src =" href" ></iframe >
2+ <iframe v-if =" href" ref = " iframe " id =" demo" class =" iframe demo-model" scrolling =" auto" frameborder =" 0" :src =" href" ></iframe >
33</template >
44
55<script setup lang="ts">
6- import { useRoute , useData } from ' vitepress' ;
7- import { computed , onMounted , watch } from ' vue'
8- const baseUrl = process .env .NODE_ENV === ' production' ? ` ${location .origin }/demo/?timestamp=${new Date ().getTime ()}#/ ` : ' http://localhost:5173/demo/#/'
6+ import { useRoute , useData } from ' vitepress'
7+ import { computed , onMounted , ref , watch } from ' vue'
8+ const baseUrl =
9+ process .env .NODE_ENV === ' production' ? ` ${location .origin }/demo/?timestamp=${new Date ().getTime ()}#/ ` : ' http://localhost:5173/demo/#/'
910
1011const route = useRoute ()
1112const href = computed (() => {
@@ -20,30 +21,34 @@ const href = computed(() => {
2021 return href
2122})
2223
24+ const iframe = ref <HTMLIFrameElement | null >(null )
25+
2326const vitepressData = useData ()
2427
2528onMounted (() => {
26- setTimeout (() => {
27- ssendMessage ()
28- }, 500 );
29- })
30-
31-
32- watch (() => vitepressData .isDark .value , () => {
33- ssendMessage ()
29+ iframe .value &&
30+ iframe .value .addEventListener (' load' , () => {
31+ // 在iframe加载完成后执行发送消息的操作
32+ ssendMessage ()
33+ })
3434})
3535
36- function ssendMessage(){
37- const iFrame: any = document .getElementById (' demo' )
38- iFrame && iFrame .contentWindow .postMessage (vitepressData .isDark .value , href .value )
36+ watch (
37+ () => vitepressData .isDark .value ,
38+ () => {
39+ ssendMessage ()
40+ }
41+ )
3942
43+ function ssendMessage() {
44+ if (iframe .value && iframe .value .contentWindow ) {
45+ iframe .value .contentWindow .postMessage (vitepressData .isDark .value , href .value )
46+ }
4047}
4148
4249function kebabToCamel(input : string ): string {
43- return input .replace (/ -([a-z ] )/ g , (match , group ) => group .toUpperCase ());
50+ return input .replace (/ -([a-z ] )/ g , (match , group ) => group .toUpperCase ())
4451}
45-
46-
4752 </script >
4853<style scoped>
4954::-webkit-scrollbar {
0 commit comments