component.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { basic } from '../mixins/basic';
  2. import { observe } from '../mixins/observer/index';
  3. function mapKeys(source, target, map) {
  4. Object.keys(map).forEach((key) => {
  5. if (source[key]) {
  6. target[map[key]] = source[key];
  7. }
  8. });
  9. }
  10. function VantComponent(vantOptions = {}) {
  11. mapKeys(vantOptions, options, {
  12. data: 'data',
  13. props: 'properties',
  14. mixins: 'behaviors',
  15. methods: 'methods',
  16. beforeCreate: 'created',
  17. created: 'attached',
  18. mounted: 'ready',
  19. relations: 'relations',
  20. destroyed: 'detached',
  21. classes: 'externalClasses'
  22. });
  23. const { relation } = vantOptions;
  24. if (relation) {
  25. options.relations = Object.assign(options.relations || {}, {
  26. [`../${relation.name}/index`]: relation
  27. });
  28. }
  29. // add default externalClasses
  30. options.externalClasses = options.externalClasses || [];
  31. options.externalClasses.push('custom-class');
  32. // add default behaviors
  33. options.behaviors = options.behaviors || [];
  34. options.behaviors.push(basic);
  35. // map field to form-field behavior
  36. if (vantOptions.field) {
  37. options.behaviors.push('wx://form-field');
  38. }
  39. // add default options
  40. options.options = {
  41. multipleSlots: true,
  42. addGlobalClass: true
  43. };
  44. observe(vantOptions, options);
  45. export default {
  46. data() {
  47. return {};
  48. },
  49. methods: {},
  50. created: function () {}
  51. };
  52. }
  53. export { VantComponent };