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
与link类似
在VUE项目中应用typescript,我们需要引入一个库vue-property-decorator,
VUE
typescript
vue-property-decorator
其是基于vue-class-component库而来,这个库vue官方推出的一个支持使用class方式来开发vue单文件组件的库
vue-class-component
vue
class
主要的功能如下:
vue-property-decorator 主要提供了多个装饰器和一个函数:
Component装饰器它注明了此类为一个Vue组件,因此即使没有设置选项也不能省略
Component
Vue
如果需要定义比如 name、components、filters、directives以及自定义属性,就可以在Component装饰器中定义,如下:
name
components
filters
directives
import {Component,Vue} from 'vue-property-decorator'; import {componentA,componentB} from '@/components'; @Component({ components:{ componentA, componentB, }, directives: { focus: { // 指令的定义 inserted: function (el) { el.focus() } } } }) export default class YourCompoent extends Vue{ }
这里取消了组件的data和methods属性,以往data返回对象中的属性、methods中的方法需要直接定义在Class中,当做类的属性和方法
@Component export default class HelloDecorator extends Vue { count: number = 123 // 类属性相当于以前的 data add(): number { // 类方法就是以前的方法 this.count + 1 } // 获取计算属性 get total(): number { return this.count + 1 } // 设置计算属性 set total(param:number): void { this.count = param } }
组件接收属性的装饰器,如下使用:
import {Component,Vue,Prop} from vue-property-decorator; @Component export default class YourComponent extends Vue { @Prop(String) propA:string; @Prop([String,Number]) propB:string|number; @Prop({ type: String, // type: [String , Number] default: 'default value', // 一般为String或Number //如果是对象或数组的话。默认值从一个工厂函数中返回 // defatult: () => { // return ['a','b'] // } required: true, validator: (value) => { return [ 'InProcess', 'Settled' ].indexOf(value) !== -1 } }) propC:string; }
实际就是Vue中的监听器,如下:
import { Vue, Component, Watch } from 'vue-property-decorator' @Component export default class YourComponent extends Vue { @Watch('child') onChildChanged(val: string, oldVal: string) {} @Watch('person', { immediate: true, deep: true }) onPersonChanged1(val: Person, oldVal: Person) {} @Watch('person') onPersonChanged2(val: Person, oldVal: Person) {} }
vue-property-decorator 提供的 @Emit 装饰器就是代替Vue 中的事件的触发$emit,如下:
@Emit
$emit
import {Vue, Component, Emit} from 'vue-property-decorator'; @Component({}) export default class Some extends Vue{ mounted(){ this.$on('emit-todo', function(n) { console.log(n) }) this.emitTodo('world'); } @Emit() emitTodo(n: string){ console.log('hello'); } }
可以看到上述typescript版本的vue class的语法与平时javascript版本使用起来还是有很大的不同,多处用到class与装饰器,但实际上本质是一致的,只有不断编写才会得心应手
vue class
javascript
The text was updated successfully, but these errors were encountered:
No branches or pull requests
一、前言
与link类似
在
VUE
项目中应用typescript
,我们需要引入一个库vue-property-decorator
,其是基于
vue-class-component
库而来,这个库vue
官方推出的一个支持使用class
方式来开发vue
单文件组件的库主要的功能如下:
二、使用
vue-property-decorator 主要提供了多个装饰器和一个函数:
@component
Component
装饰器它注明了此类为一个Vue
组件,因此即使没有设置选项也不能省略如果需要定义比如
name
、components
、filters
、directives
以及自定义属性,就可以在Component
装饰器中定义,如下:computed、data、methods
这里取消了组件的data和methods属性,以往data返回对象中的属性、methods中的方法需要直接定义在Class中,当做类的属性和方法
@props
组件接收属性的装饰器,如下使用:
@watch
实际就是
Vue
中的监听器,如下:@emit
vue-property-decorator
提供的@Emit
装饰器就是代替Vue
中的事件的触发$emit
,如下:三 、总结
可以看到上述
typescript
版本的vue class
的语法与平时javascript
版本使用起来还是有很大的不同,多处用到class
与装饰器,但实际上本质是一致的,只有不断编写才会得心应手The text was updated successfully, but these errors were encountered: