All skills
hyf0 avatar

/vue-options-api-best-practices

@bc922e4 official
by hyf0hyf0/vue-skills2.9k stars
167

Vue 3 Options API style (data(), methods, this context). Each reference shows Options API solution only.

Use this Skill: https://skilld.dev/gh/hyf0/vue-skills/vue-options-api-best-practices

This session only. Nothing lands on disk.

referenceno-arrow-functions-in-lifecycle-hooks.md

≈730 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Never Use Arrow Functions for Options API Lifecycle Hooks

Impact: HIGH - Using arrow functions for lifecycle hooks in the Options API prevents Vue from binding this to the component instance. This causes this to be undefined or reference the wrong context, leading to runtime errors when accessing component data, methods, or other properties.

Arrow functions lexically bind this from their enclosing scope. Vue's Options API lifecycle hooks (created, mounted, updated, unmounted, etc.) require regular functions so Vue can set this to the component instance at runtime.

Task Checklist

  • Always use regular function syntax for Options API lifecycle hooks
  • Use ES6 method shorthand (preferred) for cleaner code
  • Arrow functions ARE allowed inside lifecycle hooks for callbacks

Incorrect:

export default {
  data() {
    return { message: 'Hello' }
  },
  // WRONG: Arrow function - `this` will be undefined
  created: () => {
    console.log(this.message) // Error: Cannot read property 'message' of undefined
  },
  // WRONG: Arrow function for mounted
  mounted: () => {
    this.initializePlugin() // Error: this.initializePlugin is not a function
  },
  // WRONG: Arrow function for beforeUnmount
  beforeUnmount: () => {
    this.cleanup() // Will fail!
  },
  methods: {
    initializePlugin() { /* ... */ },
    cleanup() { /* ... */ }
  }
}

Correct:

export default {
  data() {
    return { message: 'Hello' }
  },
  // CORRECT: ES6 method shorthand (preferred)
  created() {
    console.log(this.message) // Works! this refers to component instance
  },
  // CORRECT: Regular function expression
  mounted: function() {
    this.initializePlugin() // Works!
  },
  // CORRECT: Method shorthand
  beforeUnmount() {
    this.cleanup() // Works!
  },
  methods: {
    initializePlugin() {
      // Arrow functions ARE fine for callbacks inside lifecycle hooks
      this.$nextTick(() => {
        this.isReady = true // Arrow inherits `this` from mounted
      })
    },
    cleanup() { /* ... */ }
  }
}

All Affected Lifecycle Hooks

The following Options API hooks must NOT use arrow functions:

  • beforeCreate
  • created
  • beforeMount
  • mounted
  • beforeUpdate
  • updated
  • beforeUnmount (Vue 3) / beforeDestroy (Vue 2)
  • unmounted (Vue 3) / destroyed (Vue 2)
  • activated
  • deactivated
  • errorCaptured
  • renderTracked
  • renderTriggered

Reference

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides comprehensive best practices and TypeScript integration guidelines for the Vue.js Options API. It contains educational content and code examples that are safe and consistent with official Vue.js documentation. No security issues were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer7mo

    11 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at bc922e4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago
version
2.0.0
author
github.com/vuejs-ai
  • Vue
  • TypeScript
  • options-api
  • best-practices
  • lifecycle-hooks
  • prop-types
  • type-safety

README badge

README badge for hyf0/vue-skills/vue-options-api-best-practices

Teaches Vue 3 Options API patterns, TypeScript integration, and common mistakes like arrow functions in methods and lifecycle hooks. Covers prop typing, event handler safety, provide/inject limitations, and computed property type inference specific to the Options API style.

Generated from the current SKILL.md.

Does this skill cover Composition API?
No. This skill focuses exclusively on Options API patterns and does not include Composition API examples or guidance.
What TypeScript support does this provide?
The skill includes references for enabling type inference with defineComponent, typing event handlers, complex prop types, provide/inject limitations, and computed property return types in the Options API context.
Does this address method binding issues?
Yes. The skill covers arrow function pitfalls in methods and lifecycle hooks, which commonly break this context binding in the Options API.

Generated from the current SKILL.md. These answers refresh after source changes.