时间:2021-05-25
npm i -s vue-property-decorator
1,@Component(options:ComponentOptions = {})
@Component 装饰器可以接收一个对象作为参数,可以在对象中声明components ,filters,directives 等未提供装饰器的选项
虽然也可以在@Component 装饰器中声明computed,watch 等,但并不推荐这么做,因为在访问this 时,编译器会给出错误提示
import { Vue, Component } from 'vue-property-decorator'@Component({ filters: { toFixed: (num: number, fix: number = 2) => { return num.toFixed(fix) } }})export default class MyComponent extends Vue { public list: number[] = [0, 1, 2, 3, 4] get evenList() { return this.list.filter((item: number) => item % 2 === 0) }}2,@Prop(options: (PropOptions | Constructor[] | Constructor) = {})
@Prop 装饰器接收一个参数,这个参数可以有三种写法:
等同于下面的js 写法
export default { props: { propA: { type: Number }, propB: { default: 'default value' }, propC: { type: [String, Boolean] } }}注意:
3,@PropSync(propName: string, options: (PropOptions | Constructor[] | Constructor) = {})
propName: string 表示父组件传递过来的属性名;
options: Constructor | Constructor[] | PropOptions 与@Prop 的第一个参数一致;
@PropSync 会生成一个新的计算属性。
等同于下面的js 写法
export default { props: { propA: { type: String, default: 'abc' } }, computed: { syncedPropA: { get() { return this.propA }, set(value) { this.$emit('update:propA', value) } } }}注意:@PropSync 需要配合父组件的.sync 修饰符使用
4,@Model(event?: string, options: (PropOptions | Constructor[] | Constructor) = {})
@Model 装饰器允许我们在一个组件上自定义v-model ,接收两个参数:
event: string 事件名。
options: Constructor | Constructor[] | PropOptions 与@Prop 的第一个参数一致。
等同于下面的js 写法
export default { model: { prop: 'value', event: 'change' }, props: { value: { type: String, default: '123' } }}上面例子中指定的是change 事件,所以我们还需要在template 中加上相应的事件:
<template> <input type="text" :value="value" @change="$emit('change', $event.target.value)" /></template>对自定义v-model 不太理解的同学,可以查看自定义事件
5,@Watch(path: string, options: WatchOptions = {})
@Watch 装饰器接收两个参数:
path: string 被侦听的属性名;
options?: WatchOptions={} options 可以包含两个属性 :
immediate?:boolean 侦听开始之后是否立即调用该回调函数;
deep?:boolean 被侦听的对象的属性被改变时,是否调用该回调函数;
侦听开始,发生在beforeCreate 勾子之后,created 勾子之前
import { Vue, Component, Watch } from 'vue-property-decorator'@Componentexport default class MyInput extends Vue { @Watch('msg') onMsgChanged(newValue: string, oldValue: string) {} @Watch('arr', { immediate: true, deep: true }) onArrChanged1(newValue: number[], oldValue: number[]) {} @Watch('arr') onArrChanged2(newValue: number[], oldValue: number[]) {}}等同于下面的js 写法
export default { watch: { msg: [ { handler: 'onMsgChanged', immediate: false, deep: false } ], arr: [ { handler: 'onArrChanged1', immediate: true, deep: true }, { handler: 'onArrChanged2', immediate: false, deep: false } ] }, methods: { onMsgVhanged(newValue, oldValue) {}, onArrChange1(newValue, oldValue) {}, onArrChange2(newValue, oldValue) {} }}6,@Emit(event?: string)
等同于下面的js 写法
export default { data() { return { count: 0 } }, methods: { addToCount(n) { this.count += n this.$emit('add-to-count', n) }, resetCount() { this.count = 0 this.$emit('reset') }, returnValue() { this.$emit('return-value', 10) }, onInputChange(e) { this.$emit('on-input-change', e.target.value, e) }, promise() { const promise = new Promise(resolve => { setTimeout(() => { resolve(20) }, 0) }) promise.then(value => { this.$emit('promise', value) }) } }}7,@Ref(refKey?: string)
@Ref 装饰器接收一个可选参数,用来指向元素或子组件的引用信息。如果没有提供这个参数,会使用装饰器后面的属性名充当参数
import { Vue, Component, Ref } from 'vue-property-decorator'import { Form } from 'element-ui'@Componentexport default class MyComponent extends Vue { @Ref() readonly loginForm!: Form @Ref('changePasswordForm') readonly passwordForm!: Form public handleLogin() { this.loginForm.validate(valide => { if (valide) { // login... } else { // error tips } }) }}等同于下面的js 写法
export default { computed: { loginForm: { cache: false, get() { return this.$refs.loginForm } }, passwordForm: { cache: false, get() { return this.$refs.changePasswordForm } } }}@Provide/@Inject 和 @ProvideReactive/@InhectReactive
由于平时基本不用到provide/inject选项,暂时先放着,以后有时间再研究
参考:https://github.com/kaorun343/...
总结
以上所述是小编给大家介绍的vue-property-decorator使用手册,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
IIS4.0系列软件最新版本下载132KBIIS4.0使用手册立即下载软件名称:IIS4.0使用手册软件版本:1.0软件大小:132KB软件授权:免费适用平台:
docutils的官方工具地址为:https://docutils.sourceforge.io/目前的更新主要是在版本和使用手册的更新上,因为Python使用
再次查找,找到了Ionic'sIsapiRewriteFilter,完全开源,完全免费,完全没有中文使用手册。测试后,使用正常,用法分享一下:首先到官方网站ht
CuteFTP使用手册CuteFTP是一个简单易用的FTP管理器,【立即下载CuteFTP】一、启动CuteFTP软件,新建站点:点击“文件&rdq
一、如何申请成为淘宝直播主播二、如何使用手机发布直播三、PC中台操作流程淘宝直播平台是阿里巴巴集团推出的官方直播购物推广平台,小编希望这个使用手册能帮助大家使用