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

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Vue + TS] Create your own Decorators in Vue with TypeScript相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)

TS 上 vue 中 props 不传递给 route.push

使用 vue-cli 3.0 创建项目

Vue3.0+TS+Element-plus实现(若依版后台管理系统)

[Javascript] Create Your First Iterator in JavaScript

[Python] Create a Log for your Python application