项目作者: justrhysism

项目描述 :
Typescript decorator for Vue mixins
高级语言: TypeScript
项目地址: git://github.com/justrhysism/vue-mixin-decorator.git
创建时间: 2018-01-20T03:40:28Z
项目社区:https://github.com/justrhysism/vue-mixin-decorator

开源协议:MIT License

下载


Vue Mixin Decorator

Build Status
npm
Greenkeeper badge
semantic-release

PLEASE NOTE: As Vue3 has moved away from class components, this project is archived and will not be recieving further updates.

This library fully depends on vue-class-component.

Most ideas and code are stolen borrowed from @HerringtonDarkholme
and his av-ts project. Also from
@JsonSong89‘s
comment, who suggested that the idea
should be extracted into a separate project which is why I’ve begrudgingly done so.

Project template shamelessly stolen from vue-property-decorator.

Best case scenario is this project/implementation/concept
gets merged/provided into/by an officially supported project
and this one can be deprecated.

License

MIT License

Install

  1. npm install --save-dev vue-mixin-decorator

Usage

There are 2 decorators:

and an extension class:

  • Mixins

Note: @Mixin is @Component exported from vue-class-component.

Single Mixin

  1. import Vue from 'vue';
  2. import { Component, Mixin, Mixins } from 'vue-mixin-decorator';
  3. @Mixin
  4. class MyMixin extends Vue {
  5. created() {
  6. console.log('Mixin created()');
  7. }
  8. mixinMethod() {
  9. console.log('Mixin method called.');
  10. }
  11. }
  12. @Component
  13. class MyComponent extends Mixins<MyMixin>(MyMixin) {
  14. created() {
  15. this.mixinMethod();
  16. }
  17. }

Multiple Mixins

  1. import Vue from 'vue';
  2. import { Component, Mixin, Mixins } from 'vue-mixin-decorator';
  3. @Mixin
  4. class MyMixin extends Vue {
  5. created() {
  6. console.log('Mixin created()');
  7. }
  8. mixinMethod() {
  9. console.log('Mixin method called.');
  10. }
  11. }
  12. @Mixin
  13. class MyOtherMixin extends Vue {
  14. created() {
  15. console.log('Other mixin created()');
  16. }
  17. otherMixinMethod() {
  18. console.log('Other mixin method called.');
  19. }
  20. }
  21. // Create an interface extending the mixins to provide
  22. interface IMixinInterface extends MyMixin, MyOtherMixin {}
  23. @Component
  24. class MyComponent extends Mixins<IMixinInterface>(MyMixin, MyOtherMixin) {
  25. created() {
  26. this.mixinMethod();
  27. this.otherMixinMethod();
  28. }
  29. }

See also