Vue Property Decorator
This library fully depends on vue-class-component, so please read its README before using this library.
License
MIT License
Install
npm i -S vue-property-decorator
Usage
There are several decorators and 1 function (Mixin):
@Prop
@PropSync
@Model
@ModelSync
@Watch
@Provide
@Inject
@ProvideReactive
@InjectReactive
@Emit
@Ref
@VModel
@Component
(provided by vue-class-component)Mixins
(the helper function namedmixins
provided by vue-class-component)
See also
@Prop(options: (PropOptions | Constructor[] | Constructor) = {})
decorator
is equivalent to
props: propA: type: Number propB: default: 'default value' propC: type: String Boolean
Note that:
type
property of each prop value from its type definition, you can use reflect-metadata.
If you'd like to set - Set
emitDecoratorMetadata
totrue
. - Import
reflect-metadata
before importingvue-property-decorator
(importingreflect-metadata
is needed just once.)
Each prop's default value need to be defined as same as the example code shown in above.
It's not supported to define each default
property like @Prop() prop = 'default value'
.
@PropSync(propName: string, options: (PropOptions | Constructor[] | Constructor) = {})
decorator
is equivalent to
props: name: type: String computed: syncedName: { return thisname } { this }
@PropSync
works like @Prop
besides the fact that it takes the propName as an argument of the decorator, and also creates a computed getter and setter behind the scenes. This way you can interface with the property as if it was a regular data property whilst making it as easy as appending the .sync
modifier in the parent component.
@Model(event?: string, options: (PropOptions | Constructor[] | Constructor) = {})
decorator
is equivalent to
model: prop: 'checked' event: 'change' props: checked: type: Boolean
@Model
property can also set type
property from its type definition via reflect-metadata
.
@ModelSync(propName: string, event?: string, options: (PropOptions | Constructor[] | Constructor) = {})
decorator
is equivalent to
model: prop: 'checked' event: 'change' props: checked: type: Boolean computed: checkedValue: { return thischecked } { this }
@ModelSync
property can also set type
property from its type definition via reflect-metadata
.
@Watch(path: string, options: WatchOptions = {})
decorator
is equivalent to
watch: child: handler: 'onChildChanged' immediate: false deep: false person: handler: 'onPersonChanged1' immediate: true deep: true handler: 'onPersonChanged2' immediate: false deep: false methods: {} {} {}
@Provide(key?: string | symbol)
/ @Inject(options?: { from?: InjectKey, default?: any } | InjectKey)
decorator
is equivalent to
const symbol = Symbol'baz' const MyComponent = Vue
@ProvideReactive(key?: string | symbol)
/ @InjectReactive(options?: { from?: InjectKey, default?: any } | InjectKey)
decorator
These decorators are reactive version of @Provide
and @Inject
. If a provided value is modified by parent component, then the child component can catch this modification.
@Emit(event?: string)
decorator
The functions decorated by @Emit
$emit
their return value followed by their original arguments. If the return value is a promise, it is resolved before being emitted.
If the name of the event is not supplied via the event
argument, the function name is used instead. In that case, the camelCase name will be converted to kebab-case.
is equivalent to
{ return count: 0 } methods: { thiscount += n this } { thiscount = 0 this } { this } { this } { const promise = { } promise }
@Ref(refKey?: string)
decorator
is equivalent to
{ anotherComponent: cache: false { return this$refsanotherComponent as AnotherComponent } button: cache: false { return this$refsaButton as HTMLButtonElement } }
@VModel(propsArgs?: PropOptions)
decorator
is equivalent to
props: value: type: String computed: name: { return thisvalue } { this }