TypeScript+Vue 插件vue-property-decorator的使用总结

首先 下载

npm install vue-class-component vue-property-decorator --save-dev

一梭子直接干;

其次,咱来说说它们的区别与联系:

vue-property-decorator社区出品;vue-class-component官方出品

vue-class-component提供了Vue、Component等;

vue-property-decorator深度依赖了vue-class-component,拓展出了更多操作符:@Prop、@Emit、@Inject、@Model、@Provide、@Watch;

开发时正常引入vue-property-decorator就行

引入后写vue代码就是如此,

import {Component, Prop, Vue} from 'vue-property-decorator'

@Component
export default class App extends Vue {
  name:string = 'Simon Zhang'

  // computed
  get MyName():string {
    return `My name is ${this.name}`
  }

  // methods
  sayHello():void {
    alert(`Hello ${this.name}`)
  }

  mounted() {
    this.sayHello();
  }
}

相当于

export default {
  data () {
    return {
      name: 'Simon Zhang'
    }
  },

  mounted () {
    this.sayHello()
  },

  computed: {
    MyName() {
      return `My name is ${this.name}`
    }
  },

  methods: {
    sayHello() {
      alert(`Hello ${this.name}`)
    },
  }
}

大佬都说爽的一批;

然鹅菜鸟我遇到问题一堆,以下做个积累总结:

1、写法问题:引入组件和接收父组件传过来的参数

@Component({
  components: {
    XXXX
  },
  props: {
    mapFlag: Number
  }
})

2、获取refs,在其后面加上as HTMLDivElement(不知道是不是这插件引起的,懒得管,直接干就是)

let layoutList:any = this.$refs.layout as HTMLDivElement
或
let fieldCalculate:any = (this as any).$refs.fieldCalculate

3、ts文件 公用方法导出

const xxx = (value: any, type: string) => {
  ...
}
export { xxx, xxx, xxx, xxx }

4、引入装饰器,使用方式@Watch

import { Component, Prop, Watch, Emit } from 'vue-property-decorator'

5、引用插件。在shims-vue.d.ts 文件中声明,再在组件中引用

declare module 'vuedraggable' {
  const vuedraggable: any;
  export default vuedraggable;
}

6、@Watch使用

// 监听formula 其变化则显示验证公式按钮
    @Watch('formula', { deep: true, immediate: true }) formulaWatch (newVal:string, oldVal:string) {
      if (newVal !== oldVal) {
       this.grammarSuccess = true
       this.errMsgFlag = false
       this.checkFormulaSuccess = false
      }
    }

7、计算属性方法写法(跟@watch一样,当成方法写就行;加一个关键字get)

get aTagDatasFcomput () { // computed计算属性, 过滤出visible为true的对象来渲染,因为当 v-if 与 v-for 一起使用时,v-for 具有比 v-if 更高的优先级,这意味着 v-if 将分别重复运行于每个 v-for 循环中
      return this.aTagDatasF.filter(item => item.visible)
}

8、@Prop的用法

@Prop({
      type: Boolean, // 父组件传递给子组件的数据类型
      required: false, // 是否必填
      default: false // 默认值, 如果传入的是 Object,则要 default: ()=>({}) 参数为函数
  }) collapsed !: boolean
@Prop()private datas!: any

感叹号是非null和非undefined的类型断言,所以上面的写法就是对datas这个属性进行非空断言

9、ts设置html的fontSize

获取时:(不加会报错Object is possibly 'null'.  真是一波骚操作)

let docEl = document.documentElement as HTMLDivElement // 加上 as HTMLDivElement

 

 

 

 

 

此文长期慢慢累积

转载于:https://my.oschina.net/lpcysz/blog/2980469

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值