uni-app.es.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { shallowRef, ref, getCurrentInstance, isInSSRComponentSetup, injectHook } from 'vue';
  2. import { hasOwn } from '@vue/shared';
  3. export { capitalize, extend, hasOwn, isPlainObject } from '@vue/shared';
  4. import { sanitise, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR, ON_SHOW, ON_HIDE, ON_LAUNCH, ON_ERROR, ON_THEME_CHANGE, ON_PAGE_NOT_FOUND, ON_UNHANDLE_REJECTION, ON_EXIT, ON_INIT, ON_LOAD, ON_READY, ON_UNLOAD, ON_RESIZE, ON_BACK_PRESS, ON_PAGE_SCROLL, ON_TAB_ITEM_TAP, ON_REACH_BOTTOM, ON_PULL_DOWN_REFRESH, ON_SAVE_EXIT_STATE, ON_SHARE_TIMELINE, ON_ADD_TO_FAVORITES, ON_SHARE_APP_MESSAGE, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED } from '@dcloudio/uni-shared';
  5. function getSSRDataType() {
  6. return getCurrentInstance() ? UNI_SSR_DATA : UNI_SSR_GLOBAL_DATA;
  7. }
  8. function assertKey(key, shallow = false) {
  9. if (!key) {
  10. throw new Error(`${shallow ? 'shallowSsrRef' : 'ssrRef'}: You must provide a key.`);
  11. }
  12. }
  13. const ssrClientRef = (value, key, shallow = false) => {
  14. const valRef = shallow ? shallowRef(value) : ref(value);
  15. // 非 h5 平台
  16. if (typeof window === 'undefined') {
  17. return valRef;
  18. }
  19. const __uniSSR = window[UNI_SSR];
  20. if (!__uniSSR) {
  21. return valRef;
  22. }
  23. const type = getSSRDataType();
  24. assertKey(key, shallow);
  25. if (hasOwn(__uniSSR[type], key)) {
  26. valRef.value = __uniSSR[type][key];
  27. if (type === UNI_SSR_DATA) {
  28. delete __uniSSR[type][key]; // TODO 非全局数据仅使用一次?否则下次还会再次使用该数据
  29. }
  30. }
  31. return valRef;
  32. };
  33. const globalData = {};
  34. const ssrRef = (value, key) => {
  35. return ssrClientRef(value, key);
  36. };
  37. const shallowSsrRef = (value, key) => {
  38. return ssrClientRef(value, key, true);
  39. };
  40. function getSsrGlobalData() {
  41. return sanitise(globalData);
  42. }
  43. /**
  44. * uni 对象是跨实例的,而此处列的 API 均是需要跟当前实例关联的,比如 requireNativePlugin 获取 dom 时,依赖当前 weex 实例
  45. */
  46. function getCurrentSubNVue() {
  47. // @ts-ignore
  48. return uni.getSubNVueById(plus.webview.currentWebview().id);
  49. }
  50. function requireNativePlugin(name) {
  51. return weex.requireModule(name);
  52. }
  53. function formatAppLog(type, filename, ...args) {
  54. // @ts-ignore
  55. if (uni.__log__) {
  56. // @ts-ignore
  57. uni.__log__(type, filename, ...args);
  58. }
  59. else {
  60. console[type].apply(console, [...args, filename]);
  61. }
  62. }
  63. function formatH5Log(type, filename, ...args) {
  64. console[type].apply(console, [...args, filename]);
  65. }
  66. function resolveEasycom(component, easycom) {
  67. return typeof component === 'string' ? easycom : component;
  68. }
  69. /// <reference types="@dcloudio/types" />
  70. const createHook = (lifecycle) => (hook, target = getCurrentInstance()) => {
  71. // post-create lifecycle registrations are noops during SSR
  72. !isInSSRComponentSetup && injectHook(lifecycle, hook, target);
  73. };
  74. const onShow = /*#__PURE__*/ createHook(ON_SHOW);
  75. const onHide = /*#__PURE__*/ createHook(ON_HIDE);
  76. const onLaunch =
  77. /*#__PURE__*/ createHook(ON_LAUNCH);
  78. const onError =
  79. /*#__PURE__*/ createHook(ON_ERROR);
  80. const onThemeChange =
  81. /*#__PURE__*/ createHook(ON_THEME_CHANGE);
  82. const onPageNotFound =
  83. /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND);
  84. const onUnhandledRejection = /*#__PURE__*/ createHook(ON_UNHANDLE_REJECTION);
  85. const onExit = /*#__PURE__*/ createHook(ON_EXIT);
  86. const onInit =
  87. /*#__PURE__*/ createHook(ON_INIT);
  88. // 小程序如果想在 setup 的 props 传递页面参数,需要定义 props,故同时暴露 onLoad 吧
  89. const onLoad =
  90. /*#__PURE__*/ createHook(ON_LOAD);
  91. const onReady = /*#__PURE__*/ createHook(ON_READY);
  92. const onUnload = /*#__PURE__*/ createHook(ON_UNLOAD);
  93. const onResize =
  94. /*#__PURE__*/ createHook(ON_RESIZE);
  95. const onBackPress =
  96. /*#__PURE__*/ createHook(ON_BACK_PRESS);
  97. const onPageScroll =
  98. /*#__PURE__*/ createHook(ON_PAGE_SCROLL);
  99. const onTabItemTap =
  100. /*#__PURE__*/ createHook(ON_TAB_ITEM_TAP);
  101. const onReachBottom = /*#__PURE__*/ createHook(ON_REACH_BOTTOM);
  102. const onPullDownRefresh = /*#__PURE__*/ createHook(ON_PULL_DOWN_REFRESH);
  103. const onSaveExitState =
  104. /*#__PURE__*/ createHook(ON_SAVE_EXIT_STATE);
  105. const onShareTimeline =
  106. /*#__PURE__*/ createHook(ON_SHARE_TIMELINE);
  107. const onAddToFavorites =
  108. /*#__PURE__*/ createHook(ON_ADD_TO_FAVORITES);
  109. const onShareAppMessage =
  110. /*#__PURE__*/ createHook(ON_SHARE_APP_MESSAGE);
  111. const onNavigationBarButtonTap = /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_BUTTON_TAP);
  112. const onNavigationBarSearchInputChanged = /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED);
  113. const onNavigationBarSearchInputClicked = /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED);
  114. const onNavigationBarSearchInputConfirmed = /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED);
  115. const onNavigationBarSearchInputFocusChanged =
  116. /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED);
  117. // for uni-app-x web
  118. const onPageHide = onHide;
  119. const onPageShow = onShow;
  120. function renderComponentSlot(slots, name, props = null) {
  121. if (slots[name]) {
  122. return slots[name](props);
  123. }
  124. return null;
  125. }
  126. export { formatAppLog, formatH5Log, getCurrentSubNVue, getSsrGlobalData, onAddToFavorites, onBackPress, onError, onExit, onHide, onInit, onLaunch, onLoad, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageHide, onPageNotFound, onPageScroll, onPageShow, onPullDownRefresh, onReachBottom, onReady, onResize, onSaveExitState, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, renderComponentSlot, requireNativePlugin, resolveEasycom, shallowSsrRef, ssrRef };