We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@prop装饰器接收一个参数,这个参数可以有三种写法:
Constructor,例如String,Number,Boolean等,指定 prop 的类型; Constructor[],指定 prop 的可选类型; PropOptions,可以使用以下选项:type,default,required,validator。
import { Vue, Component, Prop } from 'vue-property-decorator' @Component({}) export default class "组件名" extends Vue{ @Prop(Number) propA!: number; @Prop({default: 'default value'}) propB!: string; @propC([String, Boolean]) propC: string | boolean; }
等同于下面js
export default { props: { propA: { type: Number }, propB: { default: 'default value' }, propC: { type: [String, Boolean] } } }
注意: 1.属性的ts类型后面需要加上undefined类型;或者在属性名后面加上!,表示非null 和 非undefined 的断言,否则编译器会给出错误提示; 2.指定默认值必须使用上面例子中的写法,如果直接在属性名后面赋值,会重写这个属性,并且会报错。
@ProPsync装饰器与@prop用法类似,二者的区别在于:
@ProPsync 装饰器接收两个参数: propName: string 表示父组件传递过来的属性名; options: Constructor | Constructor[] | PropOptions 与@prop的第一个参数一致; @ProPsync 会生成一个新的计算属性。
import { Vue, Component, PropSync } from 'vue-property-decorator' @Component export default class MyComponent extends Vue { @PropSync('propA', { type: String, default: 'abc' }) public syncedPropA!: string }
export default { props: { propA: { type: String, default: 'abc' } }, computed: { syncedPropA: { get() { return this.propA }, set(value) { this.$emit('update:propA', value) } } } }
注意:@ProPsync需要配合父组件的.sync修饰符使用
@emit 装饰器接收一个可选参数,该参数是$Emit的第一个参数,充当事件名。如果没有提供这个参数,$Emit会将回调函数名的camelCase转为kebab-case,并将其作为事件名; @emit会将回调函数的返回值作为第二个参数,如果返回值是一个Promise对象,$emit会在Promise对象被标记为resolved之后触发; @emit的回调函数的参数,会放在其返回值之后,一起被$emit当做参数使用。
import { Vue, Component, Emit } from 'vue-property-decorator' @Component export default class MyComponent extends Vue { count = 0 @Emit() public addToCount(n: number) { this.count += n } @Emit('reset') public resetCount() { this.count = 0 } @Emit() public returnValue() { return 10 } @Emit() public onInputChange(e) { return e.target.value } @Emit() public promise() { return new Promise(resolve => { setTimeout(() => { resolve(20) }, 0) }) } }
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) }) } } }
@watch 装饰器接收两个参数:
path: string 被侦听的属性名; options?: WatchOptions={} options可以包含两个属性 : immediate?:boolean 侦听开始之后是否立即调用该回调函数; deep?:boolean 被侦听的对象的属性被改变时,是否调用该回调函数;
import { Vue, Component, Watch } from 'vue-property-decorator' @Component export default class MyInput extends Vue { @Watch('msg') public onMsgChanged(newValue: string, oldValue: string) {} @Watch('arr', { immediate: true, deep: true }) public onArrChanged1(newValue: number[], oldValue: number[]) {} @Watch('arr') public 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) {} } }
@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 } }) } }
export default { computed: { loginForm: { cache: false, get() { return this.$refs.loginForm } }, passwordForm: { cache: false, get() { return this.$refs.changePasswordForm } } } }
相关链接:https://segmentfault.com/a/1190000019906321
相关链接2:https://www.jianshu.com/p/d8ed3aa76e9b
The text was updated successfully, but these errors were encountered:
No branches or pull requests
vue使用的库:vue-property-decorator
1.@prop(options: (PropOptions | Constructor[] | Constructor) = {})
@prop装饰器接收一个参数,这个参数可以有三种写法:
Constructor,例如String,Number,Boolean等,指定 prop 的类型;
Constructor[],指定 prop 的可选类型;
PropOptions,可以使用以下选项:type,default,required,validator。
等同于下面js
注意:
1.属性的ts类型后面需要加上undefined类型;或者在属性名后面加上!,表示非null 和 非undefined
的断言,否则编译器会给出错误提示;
2.指定默认值必须使用上面例子中的写法,如果直接在属性名后面赋值,会重写这个属性,并且会报错。
2.@ProPsync(propName: string, options: (PropOptions | Constructor[] | Constructor) = {})
@ProPsync装饰器与@prop用法类似,二者的区别在于:
@ProPsync 装饰器接收两个参数:
propName: string 表示父组件传递过来的属性名;
options: Constructor | Constructor[] | PropOptions 与@prop的第一个参数一致;
@ProPsync 会生成一个新的计算属性。
等同于下面js
注意:@ProPsync需要配合父组件的.sync修饰符使用
3.@emit(event?: string)
@emit 装饰器接收一个可选参数,该参数是$Emit的第一个参数,充当事件名。如果没有提供这个参数,$Emit会将回调函数名的camelCase转为kebab-case,并将其作为事件名;
@emit会将回调函数的返回值作为第二个参数,如果返回值是一个Promise对象,$emit会在Promise对象被标记为resolved之后触发;
@emit的回调函数的参数,会放在其返回值之后,一起被$emit当做参数使用。
等同于下面js
4.@watch(path: string, options: WatchOptions = {})
@watch 装饰器接收两个参数:
path: string 被侦听的属性名;
options?: WatchOptions={} options可以包含两个属性 :
immediate?:boolean 侦听开始之后是否立即调用该回调函数;
deep?:boolean 被侦听的对象的属性被改变时,是否调用该回调函数;
等同下面js
5.@ref(refKey?: string)
@ref 装饰器接收一个可选参数,用来指向元素或子组件的引用信息。如果没有提供这个参数,会使用装饰器后面的属性名充当参数
等同于下面js
vuex使用的库:vuex-class
相关链接:https://segmentfault.com/a/1190000019906321
相关链接2:https://www.jianshu.com/p/d8ed3aa76e9b
The text was updated successfully, but these errors were encountered: