functional.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import Vue from 'vue';
  3. var inheritKey = ['ref', 'key', 'style', 'class', 'attrs', 'refInFor', 'nativeOn', 'directives', 'staticClass', 'staticStyle'];
  4. var mapInheritKey = {
  5. nativeOn: 'on'
  6. }; // inherit partial context, map nativeOn to on
  7. export function inherit(context, inheritListeners) {
  8. var result = inheritKey.reduce(function (obj, key) {
  9. if (context.data[key]) {
  10. obj[mapInheritKey[key] || key] = context.data[key];
  11. }
  12. return obj;
  13. }, {});
  14. if (inheritListeners) {
  15. result.on = result.on || {};
  16. _extends(result.on, context.data.on);
  17. }
  18. return result;
  19. } // emit event
  20. export function emit(context, eventName) {
  21. for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  22. args[_key - 2] = arguments[_key];
  23. }
  24. var listeners = context.listeners[eventName];
  25. if (listeners) {
  26. if (Array.isArray(listeners)) {
  27. listeners.forEach(function (listener) {
  28. listener.apply(void 0, args);
  29. });
  30. } else {
  31. listeners.apply(void 0, args);
  32. }
  33. }
  34. } // mount functional component
  35. export function mount(Component, data) {
  36. var instance = new Vue({
  37. el: document.createElement('div'),
  38. props: Component.props,
  39. render: function render(h) {
  40. return h(Component, _extends({
  41. props: this.$props
  42. }, data));
  43. }
  44. });
  45. document.body.appendChild(instance.$el);
  46. return instance;
  47. }