polyfill.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  2. // 小程序环境 Vue Devtools 兼容处理
  3. (function() {
  4. try {
  5. // Vue 内部 getDevtoolsGlobalHook 会获取全局对象
  6. // 并尝试访问 __VUE_DEVTOOLS_GLOBAL_HOOK__
  7. // 在小程序中,globalThis 可能为 null 或不存在
  8. // 获取全局对象的安全方式
  9. var getGlobal = function() {
  10. if (typeof globalThis !== 'undefined' && globalThis !== null) {
  11. return globalThis;
  12. }
  13. if (typeof global !== 'undefined' && global !== null) {
  14. return global;
  15. }
  16. if (typeof window !== 'undefined' && window !== null) {
  17. return window;
  18. }
  19. // 小程序环境特殊处理
  20. try {
  21. // eslint-disable-next-line no-new-func
  22. return Function('return this')();
  23. } catch (e) {
  24. return null;
  25. }
  26. };
  27. var _global = getGlobal();
  28. if (_global) {
  29. // 确保 __VUE_DEVTOOLS_GLOBAL_HOOK__ 存在
  30. if (!_global.__VUE_DEVTOOLS_GLOBAL_HOOK__) {
  31. _global.__VUE_DEVTOOLS_GLOBAL_HOOK__ = {
  32. enabled: false,
  33. addDevtools: function() {},
  34. setupDevtoolsPlugin: function() {},
  35. on: function() {},
  36. once: function() {},
  37. off: function() {},
  38. emit: function() {}
  39. };
  40. }
  41. }
  42. } catch (e) {
  43. console.warn('Polyfill __VUE_DEVTOOLS_GLOBAL_HOOK__ failed:', e);
  44. }
  45. })();
  46. // #endif