<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Documenttitle>
head>
<body>
<div>
<p>hellow worldp>
<div id="shadow">div>
div>
body>
<script>
let shadowDom = shadow.attachShadow({ mode: "closed" });
let p = document.createElement("p");
p.innerHTML = "yehuozhili";
let style = document.createElement("style");
style.textContent = `
p{color:red};
`;
shadowDom.appendChild(style);
shadowDom.appendChild(p);
script>
html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Documenttitle>
head>
<body>
<div>
<p>hellow worldp>
<div id="shadow">div>
div>
body>
<script>
class SnapshotSandbox {
constructor() {
this.proxy = window;
this.modifyPropsMap = {};
this.active(); //先走了active 并且把window上属性存到了windowsnapshot上
}
active() {
this.windowSnapshot = {};
for (const prop in window) {
if (window.hasOwnProperty(prop)) {
this.windowSnapshot[prop] = window[prop];
}
}
Object.keys(this.modifyPropsMap).forEach((p) => {//应用修改的
window[p] = this.modifyPropsMap[p];
});
}
inactive() {
//失活 就是当前和开始进入的快照进行比对,不一样就存到修改map,同时恢复开始进入的快照
for (const prop in window) {
if (window.hasOwnProperty(prop)) {//不一样的属性存到修改map里
if (window[prop] !== this.windowSnapshot[prop]) {
this.modifyPropsMap[prop] = window[prop];
window[prop] = this.windowSnapshot[prop];
}
}
}
}
}
let sandbox = new SnapshotSandbox();
((window) => {
window.a = 1;
window.b = 2;
console.log(window.a, window.b);
sandbox.inactive();
console.log(window.a, window.b);
sandbox.active();
console.log(window.a, window.b);
})(sandbox.proxy);
script>
html>
class ProxySandbox {
constructor() {
const rawWindow = window;
const fakeWindow = {}
const proxy = new Proxy(fakeWindow, {
set(target, p, value) {
target[p] = value;
return true
},
get(target, p) {
return target[p] || rawWindow[p];//这里就是取不到会找window的
}
});
this.proxy = proxy
}
}
let sandbox1 = new ProxySandbox();
let sandbox2 = new ProxySandbox();
window.a = 1;
((window) => {
window.a = 'hello';
console.log(window.a)
})(sandbox1.proxy);
((window) => {
window.a = 'world';
console.log(window.a)
})(sandbox2.proxy);
main.js
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import "element-ui/lib/theme-chalk/index.css";
import ElementUI from "element-ui";
import { registerMicroApps, start } from "qiankun";
Vue.config.productionTip = false;
Vue.use(ElementUI);
const apps = [
{
name: "vueApp", //应用名
entry: "//localhost:10000", //默认会加载这个html,解析里面的js 动态的执行(子应用必须支持跨域)
container: "#vue", //容器名
activeRule: "/vue", //激活路径
},
{
name: "reactApp",
entry: "//localhost:20000",
container: "#react",
activeRule: "/react",
},
];
registerMicroApps(apps); //注册应用
start(); //开启
new Vue({
router,
render: (h) => h(App),
}).$mount("#app");
app.vue
Home
vue
react