Code Monkey home page Code Monkey logo

Comments (5)

380815323 avatar 380815323 commented on May 18, 2024 2
  1. 子应用使用的 vite + vue3 开启单例模式,在开发环境下,切换后 组件的样式 引入 消失了,样式丢失怎么解决啊?求解。
    Snipaste_2023-03-31_19-31-35

from wujie.

yiludege avatar yiludege commented on May 18, 2024

好问题,在无界微前端中子应用代码运行在闭包内

为什么要在闭包内执行代码?

为了劫持修改 window.location,因为 location 的 configuable 为 false, 所以只能这样闭包解决

code = `(function(window, self, global, location) {
      ${code}
}).bind(window.__WUJIE.proxy)(
  window.__WUJIE.proxy,
  window.__WUJIE.proxy,
  window.__WUJIE.proxy,
  window.__WUJIE.proxyLocation,
);`;

想知道 window.location 修改了啥,可以看看 window.__WUJIE.proxyLocation

回到正题,为啥 var 不起作用呢,因为 var 在闭包内执行,怎么解决呢?看常见问题 2

有时我在想是不是出一个 api,让用户决定代码是否在 闭包内执行,毕竟这么干只是为了修改 location,用户可以直接从 window.$wujie.locaiton 拿到 修改后的 location 来用

from wujie.

squallliu avatar squallliu commented on May 18, 2024

好问题,在无界微前端中子应用代码运行在闭包内

为什么要在闭包内执行代码?

为了劫持修改 window.location,因为 location 的 configuable 为 false, 所以只能这样闭包解决

code = `(function(window, self, global, location) {
      ${code}
}).bind(window.__WUJIE.proxy)(
  window.__WUJIE.proxy,
  window.__WUJIE.proxy,
  window.__WUJIE.proxy,
  window.__WUJIE.proxyLocation,
);`;

想知道 window.location 修改了啥,可以看看 window.__WUJIE.proxyLocation

回到正题,为啥 var 不起作用呢,因为 var 在闭包内执行,怎么解决呢?看常见问题 2

有时我在想是不是出一个 api,让用户决定代码是否在 闭包内执行,毕竟这么干只是为了修改 location,用户可以直接从 window.$wujie.locaiton 拿到 修改后的 location 来用

为什么不用一个闭包,要为每个script创建一个闭包

from wujie.

yiludege avatar yiludege commented on May 18, 2024

为什么不用一个闭包,要为每个script创建一个闭包

  • 因为无界是边下载边执行的,fiber模式下,每个js文件都包裹在一个requestIdleCallback里面运行,如果要用一个闭包,那就需要先统一将所有js下载下来,然后再一个闭包内执行,效率太慢
  • 异步js文件也还是没有办法塞到这个闭包内执行

另外,如果不想js代码在闭包内执行可以 使用 jsIgnores plugin 来实现了

from wujie.

AttackXiaoJinJin avatar AttackXiaoJinJin commented on May 18, 2024
  1. 子应用使用的 vite + vue3 开启单例模式,在开发环境下,切换后 组件的样式 引入 消失了,样式丢失怎么解决啊?求解。
    Snipaste_2023-03-31_19-31-35

@380815323 试下这个:#434 (comment)

from wujie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.