JSPM

  • Created
  • Published
  • Downloads 321539
  • Score
    100M100P100Q174361F
  • License MIT

property decorators for Vue Component

Package Exports

  • vue-property-decorator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-property-decorator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Vue Property Decorator

This library fully depends on vue-class-component.

License

MIT License

Install

npm i -S vue-property-decorator

Usage

There are 3 decorators:

  • @Prop (and @prop)
  • @Watch (and @watch)
  • @Component (export Component from 'vue-class-component')
import Vue = require('vue')
import { Component Prop, Watch } from 'vue-property-decorator'

@Component
export class Component extends Vue {
  @Prop
  propA: number

  @Prop({ default: 'default value' })
  propB: string

  @Prop([String, Boolean])
  propC: string | boolean

  @Prop({ type: null })
  propD: any

  @Watch('child')
  onChildChanged(val: string, oldVal: string) { }

  @Watch('person', { immediate: true, deep: true })
  onPersonChanged(val: Person, oldVal: Person) { }
}

is equivalent to

export const Component = Vue.extend({
  name: 'Component',
  props: {
    propA: Number,
    propB: {
      type: String,
      default: 'default value'
    },
    propC: [String, Boolean],
    propD: { type: null }
  },
  methods: {
    onChildChanged(val, oldVal) { },
    onPersonChanged(val, oldVal) { }
  },
  watch: {
    'child': {
      handler: 'onChildChanged',
      immediate: false,
      deep: false
    },
    'person': {
      handler: 'onPersonChanged',
      immediate: true,
      deep: true
    }
  }
})

As you can see at propA and propB, the type can be inferred automatically when it's a simple type. For more complex types like enums you do need to specify it specifically. Also this library needs to have emitDecoratorMetadata set to true for this to work.

See also

vuex-class