网站建设 十年,网站建设课程中的收获,免费域名的选择方法,jeecms官网一、前言
与link类似
在VUE项目中应用typescript#xff0c;我们需要引入一个库vue-property-decorator#xff0c;
其是基于vue-class-component库而来#xff0c;这个库vue官方推出的一个支持使用class方式来开发vue单文件组件的库
主要的功能如下#xff1a;
metho…
一、前言
与link类似
在VUE项目中应用typescript我们需要引入一个库vue-property-decorator
其是基于vue-class-component库而来这个库vue官方推出的一个支持使用class方式来开发vue单文件组件的库
主要的功能如下
methods 可以直接声明为类的成员方法计算属性可以被声明为类的属性访问器初始化的 data 可以被声明为类属性data、render 以及所有的 Vue 生命周期钩子可以直接作为类的成员方法所有其他属性需要放在装饰器中
二、使用
vue-property-decorator 主要提供了多个装饰器和一个函数:
PropPropSyncModelWatchProvideInjectProvideReactiveInjectReactiveEmitRefComponent (由 vue-class-component 提供)Mixins (由 vue-class-component 提供)
Component
Component装饰器它注明了此类为一个Vue组件因此即使没有设置选项也不能省略
如果需要定义比如 name、components、filters、directives以及自定义属性就可以在Component装饰器中定义如下
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{}computed、data、methods
这里取消了组件的data和methods属性以往data返回对象中的属性、methods中的方法需要直接定义在Class中当做类的属性和方法
Component
export default class HelloDecorator extends Vue {count: number 123 // 类属性相当于以前的 dataadd(): number { // 类方法就是以前的方法this.count 1}// 获取计算属性get total(): number {return this.count 1}// 设置计算属性set total(param:number): void {this.count param}
}props
组件接收属性的装饰器如下使用
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;
}watch
实际就是Vue中的监听器如下
import { Vue, Component, Watch } from vue-property-decoratorComponent
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) {}
}emit
vue-property-decorator 提供的 Emit 装饰器就是代替Vue 中的事件的触发$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与装饰器但实际上本质是一致的只有不断编写才会得心应手 文章转载自: http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.hytfz.cn.gov.cn.hytfz.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.fslrx.cn.gov.cn.fslrx.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.qxmnf.cn.gov.cn.qxmnf.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.itvsee.com.gov.cn.itvsee.com http://www.morning.zjcmr.cn.gov.cn.zjcmr.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn