首页 > 其他 > 详细

[Vue + TS] Create your own Decorators in Vue with TypeScript

时间:2017-09-14 20:51:33      阅读:326      评论:0      收藏:0      [点我收藏+]

We’ve used @Watch, @Inject and more decorators from vue-property-decorator. In this lesson however we will guide you through creating your own decorators in order to add common functionality to your Vue components using TypeScript.

 

Import:

import {createDecorator} from ‘vue-class-component‘

 

Define a decorator:

const Log = (msg) => {
  return createDecorator((component, key) => {
    console.log("#Component", component);
    console.log("#Key", key); //log
    console.log("#Msg", msg); //App
  })
}

 

Using it:

  @Log(‘fullMessage get called‘)
  get fullMessage() {
    return `${this.message} from Typescript`
  }

 

Output:

#Component Object {directives: Object, components: Object, name: "Hello", methods: Object, computed: Object…}
#Key fullMessage
#Msg fullMessage get called

 

[Vue + TS] Create your own Decorators in Vue with TypeScript

原文:http://www.cnblogs.com/Answer1215/p/7522547.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!