tryOnMounted.js 604 B

12345678910111213141516171819
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.tryOnMounted = tryOnMounted;
  6. var _vue = require("vue");
  7. // eslint-disable-next-line no-restricted-imports
  8. /**
  9. * Call onMounted() if it's inside a component lifecycle, if not, just call the function
  10. *
  11. * @param fn
  12. * @param sync if set to false, it will run in the nextTick() of Vue
  13. */
  14. function tryOnMounted(fn) {
  15. let sync = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  16. if ((0, _vue.getCurrentInstance)()) (0, _vue.onMounted)(fn);else if (sync) fn();else (0, _vue.nextTick)(fn);
  17. }