Home.vue 3.29 KB
<!--
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-11-10 17:56:50
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-11-11 16:19:06
 * @FilePath: \venus\apps\frontend\micro-vue\scrm-base\src\pages\Home.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import appsManifest from '../mfe/apps.json'

const text = ref('')
function handleClick() {
  alert(`当前输入:${text.value || '(空)'}`)
}

// ------- 主应用 -> 子应用:通过 qiankun 全局状态广播 -------
const hostMsg = ref('')
const globalState = ref<any>(null)

onMounted(() => {
  const actions = (window as any).__QIANKUN_ACTIONS__
  actions?.onGlobalStateChange?.((s: any) => {
    globalState.value = s
  }, true)
})

// ------- 监听子应用挂载完成事件 -------
const lastMountedApp = ref('')
function handleMounted(e: any) {
  const detail = e?.detail || {}
  lastMountedApp.value = detail.appName || ''
}
onMounted(() => {
  window.addEventListener('mfe:mounted', handleMounted as EventListener)
})
onBeforeUnmount(() => {
  window.removeEventListener('mfe:mounted', handleMounted as EventListener)
})

function broadcastToMicro() {
  const actions = (window as any).__QIANKUN_ACTIONS__
  actions?.setGlobalState?.({ ...(globalState.value || {}), msg: hostMsg.value })
}

function syncUserToMicro() {
  const actions = (window as any).__QIANKUN_ACTIONS__
  actions?.setGlobalState?.({
    ...(globalState.value || {}),
    user: { name: 'HostUser', roles: ['admin'] },
  })
}
</script>

<template>
  <div style="padding: 24px">
    <h2 style="margin-bottom: 12px;">示例:公共基础样式组件</h2>
    <div style="display:flex; gap:12px; align-items:center;">
      <VInput v-model="text" placeholder="请输入内容" style="max-width: 240px;" />
      <VButton type="primary" @click="handleClick">提交</VButton>
    </div>

    <h2 style="margin: 24px 0 12px;">主应用向子应用广播示例</h2>
    <div style="display:flex; gap:12px; align-items:center;">
      <input
        v-model="hostMsg"
        placeholder="向子应用广播消息"
        style="padding:6px; border:1px solid #ccc; border-radius:4px; max-width: 240px;"
      />
      <button @click="broadcastToMicro" style="padding:6px 10px;">广播到子应用</button>
      <button @click="syncUserToMicro" style="padding:6px 10px;">同步用户信息</button>
      <span>当前全局消息:{{ globalState?.msg }}</span>
      <span style="margin-left:8px;">子应用列表:</span>
      <router-link
        v-for="m in appsManifest.apps"
        :key="m.dir"
        :to="m.route"
        style="margin-right:8px;"
      >打开 {{ m.displayName }}</router-link>
      <span v-if="lastMountedApp" style="margin-left:8px; color:#059669;">最近挂载:{{ lastMountedApp }}</span>
    </div>
  </div>
  
</template>

<style scoped></style>