Methods
Component(…args) → {function}
The decorator of Vue class components.
This decorator must be used to decorate a class.
Usage Example:
<template>
<div class="hello-page">
<div class="message">{{ message }}</div>
<div class="computed-message">{{ computedMessage }}</div>
<div class="value">{{ value }}</div>
<input v-model="newMessage">
<button @click="setMessage(newMessage)">Set Message</button>
<my-component />
</div>
</template>
<script>
import { Component, toVue } from 'vue3-class-component';
import MyComponent from './my-component.vue';
@Component({
components: {
MyComponent,
},
})
class HelloPage {
message = 'hello';
value = 0;
newMessage = '';
mounted() {
this.value = this.$route.params.value;
}
get computedMessage() {
return this.message + '!';
}
setMessage(s) {
this.message = s;
}
}
export default toVue(MyComponent); // don't forget calling `toVue`
</script>
- See:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any
|
<repeatable> |
The array of arguments for calling this decorator. If it has only one argument, the only argument is the additional options of this decorator, and this function should return another function which is the decorator of a class; If it has two arguments, the first argument is the constructor of the decorated class, and the second argument is the context object containing information about the class being decorated. |
Returns:
- Type:
-
function
If this function has only one argument, this function returns another function which is the decorator of a class; otherwise, this function returns the decorated class.
Inject(…args) → {function}
The @Inject decorator is marked on class fields to declare injected values.
The @Inject decorators may have an optional argument. The optional argument
is an object with the following options:
| Option | Type | Default | Description |
|---|---|---|---|
from |
String | Symbol |
undefined |
The key of the source provided value to be injected. |
default |
any |
undefined |
The default value of the injected value. |
Usage example:
@Component
class DescendantComponent {
@Inject
message;
@Inject({from: myInjectedKey, default: 0})
injectedValue;
// non-primitive default value DO NOT need to be wrapped by a factory function
@Inject({ default: {id: 0, name: 'unknown'} })
person;
}
export default toVue(DescendantComponent);
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any
|
<repeatable> |
The array of arguments for calling this decorator. If it has only one argument, the only argument is the optional options of this decorator, and this function should return another function which is the decorator of a class field; If it has two arguments, the first argument is the decorated field of a class, and the second argument is the context object containing information about the field being decorated. |
Returns:
- Type:
-
function
If this decorator has only one argument, this function returns another function which is the decorator of a class field; otherwise, this function returns the decorated class field.
Prop(…args) → {function}
The @Prop decorator is marked on class fields to declare props of the Vue
component.
The decorator may be used with an optional argument. The argument is an object containing the following options of the decorator:
| Option | Type | Default | Description |
|---|---|---|---|
type |
Function |
undefined |
The data type of the prop, which should be a constructor function, or an array of constructor functions. |
required |
Boolean |
false |
Indicates whether the prop is required or not. |
default |
any |
undefined |
Specifies the default value of the prop. |
validator |
Function |
undefined |
A custom validation function for the prop. |
Usage example:
@Component
class MyComponent {
@Prop
message = 'hello';
@Prop({ type: Number, validator: (v) => (v >= 0) })
value;
// non-primitive default value DO NOT need to be wrapped by a factory function
@Prop
person = {
id: 1,
name: 'John',
age: 32,
gender: 'MALE',
};
// multiple possible types can be represented as an array of constructors.
@Prop({ type: [Boolean, String] })
lazy;
// if the argument is a function, it will be treated as the type of the prop.
@Prop(String)
text;
// if the argument is an array of constructors, it will be treated as the
// possible types of the prop.
@Prop([String, Number])
model;
}
export default toVue(MyComponent);
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any
|
<repeatable> |
The array of arguments for calling this decorator. If it has only one argument, the only argument is the optional options of this decorator, and this function should return another function which is the decorator of a class field; If it has two arguments, the first argument is the decorated field of a class, and the second argument is the context object containing information about the field being decorated. |
Returns:
- Type:
-
function
If this decorator has only one argument, this function returns another function which is the decorator of a class field; otherwise, this function returns the decorated class field.
Provide(…args) → {function}
The @Provide decorator is marked on class fields to declare provided values
that can be injected by descendant components.
The @Provide decorators may be used with an optional argument. The optional
argument is an object with the following options:
| Option | Type | Default | Description |
|---|---|---|---|
key |
String | Symbol |
undefined |
The key of the provided value. |
reactive |
Boolean |
false |
Whether the provided value is reactive. |
Usage example:
const myInjectedKey = Symbol('myInjectedKey');
@Component
class AncestorComponent {
@Provide
message = 'hello';
@Provide({key: myInjectedKey, reactive: true})
@Prop
value = 123;
@Provide({ reactive: true })
person = {
id: 1,
name: 'John',
age: 32,
gender: 'MALE',
};
}
export default toVue(AncestorComponent);
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any
|
<repeatable> |
The array of arguments for calling this decorator. If it has only one argument, the only argument is the optional options of this decorator, and this function should return another function which is the decorator of a class field; If it has two arguments, the first argument is the decorated field of a class, and the second argument is the context object containing information about the field being decorated. |
Returns:
- Type:
-
function
If this decorator has only one argument, this function returns another function which is the decorator of a class field; otherwise, this function returns the decorated class field.
Raw(target, context) → {function|void}
The @Raw decorator is marked on class fields to declare the field as a raw
field, which is not reactive.
Usage example:
@Component
class MyComponent {
message = 'hello';
@Raw
rawValue = 123;
@Raw
person = {
id: 1,
name: 'John',
age: 32,
gender: 'MALE',
};
}
export default toVue(MyComponent);
The above code declares a Vue component named MyComponent with three fields:
messageis a reactive field with the initial value'hello'.rawValueis a non-reactive raw field with the initial value123.personis a non-reactive raw field with the initial value:
Parameters:
| Name | Type | Description |
|---|---|---|
target |
string
|
The target of the decorator. Since this decorator is used to decorate a
class field, the target is always |
context |
object
|
The context object containing information about the field being decorated. |
Returns:
- Type:
-
function|void
An initializer function which runs when the field is assigned, receiving the initial value of the field and returning a new initial value; or returns nothing.
VModel(…args) → {function}
The @VModel decorator is marked on class fields to declare v-model bounded
props of the Vue component.
See Component v-model
for more details about the v-model binding.
The decorator may be used with an optional argument. The argument is an object containing the following options of the decorator:
| Option | Type | Default | Description |
|---|---|---|---|
type |
Function |
undefined |
The data type of the prop, which should be a constructor function. |
required |
Boolean |
false |
Indicates whether the prop is required or not. |
default |
any |
undefined |
Specifies the default value of the prop. |
validator |
Function |
undefined |
A custom validation function for the prop. |
Usage example:
<template>
<div class="my-component">
<input v-model="message" />
</div>
</template>
<script>
import { Component, VModel, toVue } from '@haixing_hu/vue3-class-component';
@Component
class MyComponent {
@VModel({ type: String, validator: (v) => (v.length >= 0) })
message;
}
export default toVue(MyComponent);
</script>
The @VModel prop in the component defined above can be used as follows:
<template>
<div class="use-my-component">
<my-component v-model="msg" />
</div>
</template>
- See:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any
|
<repeatable> |
The array of arguments for calling this decorator. If it has only one argument, the only argument is the optional options of this decorator, and this function should return another function which is the decorator of a class field; If it has two arguments, the first argument is the decorated field of a class, and the second argument is the context object containing information about the field being decorated. |
Returns:
- Type:
-
function
If this decorator has only one argument, this function returns another function which is the decorator of a class field; otherwise, this function returns the decorated class field.
Watch(path, options) → {function}
The @Watch decorator is marked on class methods to declare watchers of the
Vue component.
The @Watch decorator has one or two arguments. The first argument of the
@Watch decorator is the path of the watched states or watched properties.
The second optional argument of the @Watch decorator is an object with the
following options:
| Option | Type | Default | Description |
|---|---|---|---|
deep |
Boolean |
false |
Whether the watcher should deep traversal of the source if it is an object or an array. |
immediate |
Boolean |
false |
Whether the watcher should be called immediately after the watcher is created. |
flush |
String |
'pre' |
The flushing timing of the watcher. It can be one of 'pre', 'post' or 'sync'. |
Usage example:
@Component
class MyComponent {
value = 123;
person = {
id: 1,
name: 'John',
age: 32,
gender: 'MALE',
};
@Watch('value')
onValueChanged(val, oldVal) {
console.log(`The value is changed from ${oldVal} to ${val}.`);
}
@Watch('person', { deep: true })
onPersonChanged(val, oldVal) {
console.log(`The person is changed from ${oldVal} to ${val}.`);
}
}
export default toVue(MyComponent);
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string
|
The path of the watched states or watched properties. |
options |
object
|
The optional options of the |
Returns:
- Type:
-
function
The decorator of a class method.
createDecorator(factory) → {function}
Creates a customized decorator to extends the functionality of this library.
An example usage is as follows:
const Log = createDecorator((Class, defaultInstance, target, context, options) => {
if (context?.kind !== 'method') {
throw new Error('The @Log decorator can only be used to decorate a class method.');
}
const methodName = context.name;
const originalMethod = options.methods[methodName];
options.methods[methodName] = function (...args) {
console.log(`${Class.name}.${methodName}: ${args.join(', ')}`);
return originalMethod.apply(this, args);
};
});
The above example creates a @Log decorator which can be used to log the
arguments of a class method. For example:
@Component
class HelloPage {
message = 'hello';
value = 0;
newMessage = '';
@Log
mounted() {
this.value = this.$route.params.value;
}
get computedMessage() {
return this.message + '!';
}
@Log
setMessage(s) {
this.message = s;
}
}
export default toVue(MyComponent);
- See:
Parameters:
| Name | Type | Description |
|---|---|---|
factory |
function
|
A callback function which receives the following arguments:
|
Returns:
- Type:
-
function
The customized decorator function, which takes two arguments:
target: The target value being decorated, which could be class method, class field, getter or setter.context: The context object containing information about the target being decorated.
toVue(Class) → {Object}
Converts a Vue class component into the Vue component option.
Parameters:
| Name | Type | Description |
|---|---|---|
Class |
The class. |
Returns:
- Type:
-
Object
The corresponding Vue component option.