Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-decorator

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-decorator

Custom decorators to vue-class-component that fits Vue 3

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
414
decreased by-2.36%
Maintainers
1
Weekly downloads
 
Created
Source

vue-class-decorator

Custom decorators to vue-class-component that fits Vue 3, heavily inspired by vue-property-decorator


Help need: yes

##Compatible: Vue 3

License

MIT License

Install

yarn

yarn add vue-decorator

npm

npm i vue-decorator

Usage

@Prop(options: (PropOptions | VueConstructor[] | VueConstructor) = {})

import {Vue, Options, Prop} from 'vue-decorator'

@Options({})
export default class YourComponent extends Vue {
    @Prop(Number) readonly propA: number | undefined
    @Prop({default: 'default value'}) readonly propB!: string
    @Prop([String, Boolean]) readonly propC: string | boolean | undefined
}

is equivalent to

export default {
    props: {
        propA: {
            type: Number,
        },
        propB: {
            default: 'default value',
        },
        propC: {
            type: [String, Boolean],
        },
    },
}

@Watch(path: string, options: WatchOptions = {})

import {Vue, Options, Watch} from 'vue-decorator'

@Options({})
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) {
    }
}

is equivalent to

export default {
    name: 'MyComponent',
    watch: {
        child: [
            {
                handler: 'onChildChanged',
                immediate: false,
                deep: false,
            },
        ],
        person: [
            {
                handler: 'onPersonChanged1',
                immediate: true,
                deep: true,
            },
            {
                handler: 'onPersonChanged2',
                immediate: false,
                deep: false,
            },
        ],
    },
    methods: {
        onChildChanged(val, oldVal) {
        },
        onPersonChanged1(val, oldVal) {
        },
        onPersonChanged2(val, oldVal) {
        },
    },
}

@Emit(event?: string) decorator

The functions decorated by @Emit $emit their return value followed by their original arguments. If the return value is a promise, it is resolved before being emitted.

If the name of the event is not supplied via the event argument, the function name is used instead. In that case, the camelCase name will be converted to kebab-case.

import { Vue, Options, Emit } from 'vue-decorator'

@Options({})
export default class YourComponent extends Vue {
  count = 0

  @Emit()
  addToCount(n: number) {
    this.count += n
  }

  @Emit('reset')
  resetCount() {
    this.count = 0
  }

  @Emit()
  returnValue() {
    return 10
  }

  @Emit()
  onInputChange(e) {
    return e.target.value
  }

  @Emit()
  promise() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(20)
      }, 0)
    })
  }
}

is equivalent to

export default {
  data() {
    return {
      count: 0,
    }
  },
  methods: {
    addToCount(n) {
      this.count += n
      this.$emit('add-to-count', n)
    },
    resetCount() {
      this.count = 0
      this.$emit('reset')
    },
    returnValue() {
      this.$emit('return-value', 10)
    },
    onInputChange(e) {
      this.$emit('on-input-change', e.target.value, e)
    },
    promise() {
      const promise = new Promise((resolve) => {
        setTimeout(() => {
          resolve(20)
        }, 0)
      })

      promise.then((value) => {
        this.$emit('promise', value)
      })
    },
  },
}

@Ref(refKey?: string)

import { Vue, Options, Ref } from 'vue-decorator'

@Options({})
export default class YourComponent extends Vue {
  @Ref('aButton') readonly button!: HTMLButtonElement
}

is equivalent to

export default {
  computed() {
    button: {
      cache: false,
      get() {
        return this.$refs.aButton as HTMLButtonElement
      }
    }
  }
}

Keywords

FAQs

Package last updated on 03 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc