官方文档:https://wujie-micro.github.io/doc/
安装
npm i wujie-vue2 -S
javascript
父级容器使用:
main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import WujieVue from "wujie-vue2";
const { bus, setupApp, preloadApp, destroyApp } = WujieVue;
Vue.use(WujieVue);
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
javascript
使用页面
假如有2个子项目 为app1和app2
父级app2.vue使用 打开的页面:http:
<WujieVue width="100%" height="100%" name="app2" url="http://localhost:8082" :props="{jump,data: 123, methods: fn}" :alive="true" :sync="true"></WujieVue>
js
methods: {
fn(e){
alert(e)
},
jump(location, query) {
this.$router.push(location);
const url = new URL(window.location.href);
url.search = query
window.history.replaceState(null, "", url.href);
}
},
javascript
WujieVue 配置选项
看情况开启
开了麻烦点
fn自定义方法
jump() 对应者app1 应用想跳到 app2应用或者其他应用或者页面
子项目使用:
子项目 不用安装依赖
main.js 需要添加代码
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
Vue.config.productionTip = false
if (window.__POWERED_BY_WUJIE__) {
let instance;
window.__WUJIE_MOUNT = () => {
instance = new Vue({ router, store, render: (h) => h(App) }).$mount("#app");
};
window.__WUJIE_UNMOUNT = () => {
instance.$destroy();
};
} else {
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
}
javascript
子项目页面跳转
app2想跳转到app1项目 就用 window.$wujie && $wujie.props.jump({ path: “/app1” });
jump 事件是在 父级容器 :props=“{jump}” 绑定的
<template>
<div>子应用APP2的app22
<button @click="handleJump">点击我 去子应用APP2的app23</button>
<button @click="handleJump2">点击我 去/app1应用</button>
<h1>是否保活{{num}}</h1>
</div>
</template>
<script>
export default {
methods: {
handleJump() {
this.$router.push({ path: "/app23" });
},
handleJump2() {
window.$wujie && $wujie.props.jump({ path: "/app1" });
}
},
created() {
const props = window.$wujie?.props;
setTimeout(() => {
props.methods(666)
},2000)
setInterval(() => {
this.num++
}, 1000)
},
mounted() {},
destroyed() {}
};
</script>
javascript
子应用 app1 点击跳转处理函数 跳转 保活模式
复杂点的就是app1页面 跳转保活模式app2的页面
如果子应用 app2 是保活应用并且没有被打开过,也就是还没有实例化,上述的打开指定路由的方式可以正常工作,但如果子应用 app2 已经实例化,保活应用的内部数据和路由状态都会保存下来不随子应用切换而丢失。
这时如果要打开子应用 app2 的指定路由可以使用通信的方式 :
需要父级去做一个跳板 先让父元素跳转到/app2 父元素再让app2的应用去跳转对应的路由
子应用 app1
function handleJump() {
window.$wujie && window.$wujie.bus.$emit("routeChange", ['app2', '/app2', '/app22', { wsd: 895 }]);
}
父级容器:去监听A应用的跳转事件
WujieVue.bus.$on('routeChange', (arr) => {
this.$router.push({ path: arr[1] })
WujieVue.bus.$emit(`${arr[0]}:routeChange`, { path: arr[2] || '', query: arr[3] || '' })
})
javascript
子应用 app2
window.$wujie && window.$wujie.bus.$on("app2:routeChange", (data) => {
if (!data.path || (this.$route.path == data.path)) return
this.$router.push(data)
});
javascript
APP1应用跳转APP2实践代码
app1的项目 index.vue
<template>
<div>app1
<button @click="handleJump">跳转到app2应用 默认路由</button>
<button @click="handleJump2">跳转到app2应用 默认路由(保活模式)</button>
<br>
<br>
<button @click="handleJump3">跳转app2应用的指定目录=>/app22</button>
<button @click="handleJump4">跳转app2应用的指定目录=>/app22(保活模式)</button>
</div>
</template>
<script>
export default {
methods: {
handleJump() {
window.$wujie && $wujie.props.jump({ path: "/app2" });
},
handleJump2() {
window.$wujie && window.$wujie.bus.$emit("routeChange", ['app2', '/app2', '/', {}]);
},
handleJump3() {
window.$wujie && $wujie.props.jump({ path: "/app2", query: { app2: "/app22?sss=222" }, params: { a: 1 } });
},
handleJump4() {
window.$wujie && window.$wujie.bus.$emit("routeChange", ['app2', '/app2', '/app22', { wsd: 895 }]);
}
},
};
</script>
javascript
父元素处理 app1.vue (正常jump跳转处理)
<template>
<div class="box_app">
这是主应用 app1的页面 下面是 app1的页面子应用
<br>
👇
<WujieVue width="100%" height="20%" name="app1" url="http://localhost:8081" :props="{jump}" :sync="true"></WujieVue>
</div>
</template>
<script>
export default {
methods: {
jump(location, query) {
console.log(location);
// 跳转到主应用B页面
this.$router.push(location);
const url = new URL(window.location.href);
url.search = query
// 手动的挂载url查询参数
window.history.replaceState(null, "", url.href);
}
},
}
</script>
<style scoped>
.box_app {
height: 1000px;
width: 1000px;
padding: 50px;
border: 2px solid #ccc;
}
</style>
javascript
父元素 App.vue (保活模式处理)
created() {
WujieVue.bus.$on('routeChange', (arr) => {
this.$router.push({ path: arr[1] })
WujieVue.bus.$emit(`${arr[0]}:routeChange`, { path: arr[2] || '', query: arr[3] || '' })
})
},
javascript
app2页面处理
如果是不保活的 不用处理
保活的需要处理 bus.$on事件
mounted() {
window.$wujie && window.$wujie.bus.$on("app2:routeChange", (data) => {
console.log(data);
if (!data.path || (this.$route.path == data.path)) return
this.$router.push(data)
});
},
javascript
第一次进入的时候 携带参数 可以用window.$wujie?.props
或者,window.$wujie && $wujie.props.jump({ path: "/app2", query: { app2: "/app22?sss=222" }, params: { a: 1 } },);
javascript
跳转路由的时候携带参数 query
params还不知道怎么携带过去
还是有很多问题的 比如如何判断 跳转的应用 是否是保活的 采取那种跳转情况
TODO需要在父元素 判断…
官方文档:https://wujie-micro.github.io/doc/
安装
父级容器使用:
main.js import Vue from 'vue' import App from './App.vue' import router from './router' import store from './store' import WujieVue from "wujie-vue2"; const { bus, setupApp, preloadApp, destroyApp } = WujieVue; Vue.use(WujieVue); Vue.config.productionTip = false new Vue({ router, store, render: h => h(App) }).$mount('#app') javascript使用页面
假如有2个子项目 为app1和app2
父级app2.vue使用 打开的页面:http://localhost:8082 name=app2 <WujieVue width="100%" height="100%" name="app2" url="http://localhost:8082" :props="{jump,data: 123, methods: fn}" :alive="true" :sync="true"></WujieVue> js methods: { fn(e){ alert(e) }, jump(location, query) { // 跳转到主应用B页面 this.$router.push(location); const url = new URL(window.location.href); url.search = query // 手动的挂载url查询参数 window.history.replaceState(null, "", url.href); } }, javascriptWujieVue 配置选项
看情况开启
开了麻烦点
fn自定义方法
jump() 对应者app1 应用想跳到 app2应用或者其他应用或者页面
子项目使用:
子项目 不用安装依赖
main.js 需要添加代码 import Vue from 'vue' import App from './App.vue' import router from './router' import store from './store' Vue.config.productionTip = false if (window.__POWERED_BY_WUJIE__) { let instance; window.__WUJIE_MOUNT = () => { instance = new Vue({ router, store, render: (h) => h(App) }).$mount("#app"); }; window.__WUJIE_UNMOUNT = () => { instance.$destroy(); }; } else { new Vue({ router, store, render: h => h(App) }).$mount('#app') } javascript子项目页面跳转
app2想跳转到app1项目 就用 window.$wujie && $wujie.props.jump({ path: “/app1” });
jump 事件是在 父级容器 :props=“{jump}” 绑定的
子应用 app1 点击跳转处理函数 跳转 保活模式
复杂点的就是app1页面 跳转保活模式app2的页面
如果子应用 app2 是保活应用并且没有被打开过,也就是还没有实例化,上述的打开指定路由的方式可以正常工作,但如果子应用 app2 已经实例化,保活应用的内部数据和路由状态都会保存下来不随子应用切换而丢失。
这时如果要打开子应用 app2 的指定路由可以使用通信的方式 :
需要父级去做一个跳板 先让父元素跳转到/app2 父元素再让app2的应用去跳转对应的路由
子应用 app1
// 子应用 app1 点击跳转处理函数 function handleJump() { window.$wujie && window.$wujie.bus.$emit("routeChange", ['app2', '/app2', '/app22', { wsd: 895 }]); } 父级容器:去监听A应用的跳转事件 WujieVue.bus.$on('routeChange', (arr) => { this.$router.push({ path: arr[1] }) WujieVue.bus.$emit(`${arr[0]}:routeChange`, { path: arr[2] || '', query: arr[3] || '' }) }) javascript子应用 app2
// 子应用 app2 监听并跳转 window.$wujie && window.$wujie.bus.$on("app2:routeChange", (data) => { if (!data.path || (this.$route.path == data.path)) return this.$router.push(data) }); javascriptAPP1应用跳转APP2实践代码
父元素处理 app1.vue (正常jump跳转处理)
父元素 App.vue (保活模式处理)
created() { WujieVue.bus.$on('routeChange', (arr) => { this.$router.push({ path: arr[1] }) WujieVue.bus.$emit(`${arr[0]}:routeChange`, { path: arr[2] || '', query: arr[3] || '' }) }) }, javascriptapp2页面处理
如果是不保活的 不用处理
保活的需要处理 bus.$on事件
mounted() { window.$wujie && window.$wujie.bus.$on("app2:routeChange", (data) => { console.log(data); if (!data.path || (this.$route.path == data.path)) return this.$router.push(data) }); }, javascript第一次进入的时候 携带参数 可以用window.$wujie?.props 或者,window.$wujie && $wujie.props.jump({ path: "/app2", query: { app2: "/app22?sss=222" }, params: { a: 1 } },); javascript跳转路由的时候携带参数 query
params还不知道怎么携带过去
还是有很多问题的 比如如何判断 跳转的应用 是否是保活的 采取那种跳转情况
TODO需要在父元素 判断…
微信扫一扫
二维码便可将本文分享至朋友圈。