
Vue Options Api Best Practices
Avoid broken `this` in Vue Options API by using correct lifecycle hook syntax when the agent writes or reviews Vue 2/3 components.
Overview
vue-options-api-best-practices is an agent skill for the Build phase that prevents Options API lifecycle hooks from breaking `this` binding by banning arrow functions on hook properties.
Install
npx skills add https://github.com/hyf0/vue-skills --skill vue-options-api-best-practicesWhat is this skill?
- HIGH impact rule: never use arrow functions as Options API lifecycle hooks (created, mounted, updated, unmounted)
- Explains lexical `this` binding vs Vue’s runtime component instance binding
- Prefers ES6 method shorthand for lifecycle hooks
- Allows arrow functions inside hooks for callbacks only
- Task checklist for auditing hook definitions before merge
- HIGH impact lifecycle binding rule
- Task checklist with 3 audit items
Adoption & trust: 1.8k installs on skills.sh; 2.5k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Vue component throws errors in created or mounted because the agent used arrow functions and `this` no longer points at the component instance.
Who is it for?
Indie devs on Options API Vue apps who want the coding agent to follow one high-impact correctness rule automatically.
Skip if: Composition API-only projects or teams standardized on `<script setup>` without Options API lifecycle hooks.
When should I use this skill?
Writing or reviewing Vue Options API components that define created, mounted, updated, or unmounted hooks.
What do I get? / Deliverables
Lifecycle hooks use valid function syntax so `this` resolves to the component instance and initialization code runs reliably.
- Corrected lifecycle hook definitions
- Review checklist completion for hook syntax
Recommended Skills
Journey fit
How it compares
Use as a narrow linter-style guardrail instead of relying on the model’s generic Vue knowledge during codegen.
Common Questions / FAQ
Who is vue-options-api-best-practices for?
Solo builders and small teams still using Vue Options API in Vue 2 or Vue 3 who delegate component writes to Claude Code, Cursor, or Codex.
When should I use vue-options-api-best-practices?
Invoke it whenever the agent adds or edits created, mounted, updated, or unmounted handlers on `export default` objects.
Is vue-options-api-best-practices safe to install?
It is documentation-only guidance with no shell or network requirements; confirm package integrity via the Security Audits panel on this page.
SKILL.md
READMESKILL.md - Vue Options Api Best Practices
# 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:** ```javascript 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:** ```javascript 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 - [Vue.js Lifecycle Hooks](https://vuejs.org/guide/essentials/lifecycle.html) - [Vue.js Options Lifecycle](https://vuejs.org/api/options-lifecycle.html) --- title: Never Use Arrow Functions in Methods Option impact: HIGH impactDescription: Arrow functions prevent Vue from binding `this` to the component instance type: capability tags: [vue3, vue2, options-api, methods, this-binding] --- # Never Use Arrow Functions in Methods Option **Impact: HIGH** - Using arrow functions in the `methods` option causes `this` to be `undefined` or the wrong context, leading to runtime errors when trying to access component data, computed properties, or other methods. Arrow functions lexically bind `this` from their enclosing scope, not from the object they're defined on. Vue's `methods` option requires regular functions so Vue can bind `this` to the component instance. ## Task Checklist - [ ] Always use regular function syntax for methods in Options API - [ ] If using ES6 shorthand, use method shorthand (preferred) - [ ] Arrow functions ARE allowed inside methods for callbacks **Incorrect:** ```javascript export default { data() { return { count: 0 } }, methods: { // WRONG: Arrow function - `this` will be undefin