环境
- vue:
^3.3.4
实战
发送端(即触发站点)
在App.vue
中引入CrossDomainStorage
组件(后面有实现过程)
<script setup>
import { ref } from 'vue'
import CrossDomainStorage from "@/components/CrossDomainStorage/index.vue";
const crossDomainStorageRef = ref(null)function sendTest() {if (crossDomainStorageRef.value){crossDomainStorageRef.value.sendMessage("getItem", {key:'APP_THEME_SCREEN'})}
}
</script><template><div><button @click="sendTest">测试</button><CrossDomainStorage :src="'http://xxxx.xxx/'" ref="crossDomainStorageRef"/></div>
</template>
接收端(即目标站点)
为了方便直接在App.vue
中实践
<script setup>
import ParentMsgListener from '@/utils/parentMsgListener'const parentMsgListener = new ParentMsgListener([ // 设置可操作的对象列表'localStorage'
])
parentMsgListener.start();
</script>
实现代码
CrossDomainStorage
组件
<script setup lang="ts">
import {ref, reactive, onMounted} from 'vue';defineOptions({name: 'CrossDomainStorage'
});const iframeRef = ref();
const emit = defineEmits(['onLoad','response'