From 633ddfbd1a0a83f549553d8467241609037a5f78 Mon Sep 17 00:00:00 2001 From: YM <479443481@qq.com> Date: 星期六, 11 五月 2024 17:03:07 +0800 Subject: [PATCH] 组件通信 --- vite.config.ts | 10 ++-- src/qiankunState/state.ts | 6 +++ electron/main.ts | 2 src/child.ts | 2 src/main.ts | 41 ++++++++++++++++---- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/electron/main.ts b/electron/main.ts index e1a7f61..db462f2 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -48,7 +48,7 @@ require('electron-reload')('../', { electron: require(elePath) }) - win.loadURL('http://localhost:8005') + win.loadURL('http://192.168.3.203:8005') win.webContents.openDevTools() } else { win.loadURL(path.join(__dirname, 'index.html')) diff --git a/src/child.ts b/src/child.ts index 4b172f9..485662e 100644 --- a/src/child.ts +++ b/src/child.ts @@ -21,7 +21,7 @@ name: 'app-content', // - 蹇呴�夛紝寰簲鐢ㄧ殑鍏ュ彛 entry: '//182.92.203.7:3007/books/test/', - // entry: '//192.168.3.10:8080', + // entry: '//192.168.3.203:8080', // - 蹇呴�夛紝寰簲鐢ㄧ殑瀹瑰櫒鑺傜偣鐨勯�夋嫨鍣ㄦ垨鑰� Element 瀹炰緥 container: '#container', // - 蹇呴�夛紝寰簲鐢ㄧ殑婵�娲昏鍒� diff --git a/src/main.ts b/src/main.ts index 1f982dc..2e71243 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,11 +8,11 @@ import router from './router' import * as ElementPlusIconsVue from '@element-plus/icons-vue' import toolClass from '@/assets/js/toolClass' -import request from "@/plugin/axios/index.ts" -import "./child.ts" +import request from '@/plugin/axios/index.ts' +import './child.ts' const handleGetToken = () => { - return localStorage.getItem("token"); + return localStorage.getItem('token') } // 璺敱鎵ц涔嬪墠鐨勪竴浜涙搷浣� @@ -20,22 +20,22 @@ // 濡傛灉鏈塼oken if (handleGetToken()) { // 鏄惁鏄櫥褰曢〉闈紝鐩存帴鍒伴椤� - if (to.path === "/login") { - next({ path: "/transmission" }); + if (to.path === '/login') { + next({ path: '/transmission' }) } else { // 濡傛灉涓嶆槸鐧诲綍椤甸潰锛岃烦杞埌鐩爣鐨勯〉闈� - next(); + next() } } else { // 娌℃湁token if (!to.meta || !to.meta.auth) { // 鍦ㄥ厤鐧诲綍鐧藉悕鍗曪紝鐩存帴杩涘叆 - next(); + next() } else { - next(`/login?redirect=${to.fullPath}`); // 鍚﹀垯鍏ㄩ儴閲嶅畾鍚戝埌鐧诲綍椤� + next(`/login?redirect=${to.fullPath}`) // 鍚﹀垯鍏ㄩ儴閲嶅畾鍚戝埌鐧诲綍椤� } } -}); +}) const app = createApp(App) app.provide('toolClass', toolClass) @@ -48,3 +48,26 @@ } app.mount('#parentApp') + +// qiankun涓诲簲鐢ㄤ笌寰簲鐢ㄩ�氳 +import { initGlobalState, type MicroAppStateActions } from 'qiankun' +import state from './qiankunState/state' + +state.bb = (data) => { + console.log(data); +} + +// 鍒濆鍖� state +const actions: any = initGlobalState(state) +// 鐩戝惉state鍙樺寲 +actions.onGlobalStateChange((state: any, prev: any) => { + // state: 鍙樻洿鍚庣殑鐘舵��; prev 鍙樻洿鍓嶇殑鐘舵�� + console.log("鐖跺眰change锛�",state, prev) +}) +// 璁剧疆state鐨勫�� +actions.setGlobalState(state) + +console.log(actions,"actions"); + +// 绉婚櫎褰撳墠搴旂敤鐨勭姸鎬佺洃鍚紝寰簲鐢� umount 鏃朵細榛樿璋冪敤 +// actions.offGlobalStateChange() diff --git a/src/qiankunState/state.ts b/src/qiankunState/state.ts new file mode 100644 index 0000000..8f7e4ee --- /dev/null +++ b/src/qiankunState/state.ts @@ -0,0 +1,6 @@ +const state = { + aa: 1 +} + + +export default state \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index ed7aa8f..e11d194 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -9,10 +9,10 @@ base:"./", plugins: [ vue(), - // electron({ - // // 閰嶇疆 Electron 鍏ュ彛鏂囦欢 - // entry: 'electron-commonJS/main.js' - // }), + electron({ + // 閰嶇疆 Electron 鍏ュ彛鏂囦欢 + entry: 'electron-commonJS/main.js' + }), ], resolve: { alias: { @@ -26,7 +26,7 @@ cors: true, // 鍏佽璺ㄥ煙 hmr: true, headers: { - "Access-Control-Allow-Origin": "*", // 鍏佽璺ㄥ煙璁块棶瀛愬簲鐢ㄩ〉闈� + "access-control-allow-origin": "*", // 鍏佽璺ㄥ煙璁块棶瀛愬簲鐢ㄩ〉闈� }, }, define: { -- Gitblit v1.9.1