Vue Functions
This package, `vue-functions` (current stable version 2.0.6, last published in June 2020), provides a collection of utility functions and mixins specifically designed for Vue.js 2 applications. It offers features such as `updatablePropsEvenUnbound` for managing component props, `watchAsync` for watching asynchronous dependencies, reactive `windowSize` access via a mixin, and a `hookHelper` for extending component lifecycle hooks. Due to its foundational reliance on Vue 2's mixin system, it is not directly compatible with Vue 3's Composition API without significant refactoring or the use of Vue's compatibility build. The package's release cadence appears irregular, and its primary utility lies in streamlining common patterns within Vue 2 projects. Vue 2 itself officially reached End-of-Life on December 31st, 2023, making this package primarily relevant for maintaining legacy Vue 2 applications.
Common errors
-
SyntaxError: require() of ES Module 'vue-functions' from ... not supported
cause Attempting to use CommonJS `require()` syntax in a modern JavaScript module context (ESM) which `vue-functions` is likely bundled as, or when your build system expects ESM.fixUse ES Module `import` syntax instead: `import * as vf from 'vue-functions'`. -
[Vue warn]: Unknown custom element: <my-component> - did you register the component correctly?
cause A Vue component or mixin from `vue-functions` was used without proper global or local registration within a Vue application. This can happen if Vue is not globally available or if components are not declared in `components` option.fixEnsure Vue is imported (`import Vue from 'vue'`) and available, and that components/mixins are correctly applied (e.g., using the `mixins` option in a Vue component). -
TypeError: Cannot read properties of undefined (reading 'mixins')
cause This typically occurs when attempting to use a Vue mixin or component options outside of a properly instantiated Vue component or Vue.extend() call, or if Vue is not correctly set up.fixEnsure `vue-functions` mixins are applied within a valid Vue component definition, for example: `export default Vue.extend({ mixins: [someMixin] })`.
Warnings
- breaking Vue 2 reached End-of-Life (EOL) on December 31st, 2023. This package, designed for Vue 2, will likely not receive further updates or critical bug fixes.
- gotcha This library is fundamentally built on Vue 2's Options API and mixin system. It is not directly compatible with Vue 3's Composition API or `script setup` syntax without significant refactoring or leveraging the `@vue/compat` migration build.
- gotcha The `watchAsync` utility has minimal to no official documentation beyond a note to 'check source'. This makes its usage and behavior less predictable and harder to debug without direct code inspection.
Install
-
npm install vue-functions -
yarn add vue-functions -
pnpm add vue-functions
Imports
- * as vf
const vf = require('vue-functions')import * as vf from 'vue-functions'
- updatablePropsEvenUnbound
import { updatablePropsEvenUnbound } from 'vue-functions' - windowSize
import { windowSize } from 'vue-functions'
Quickstart
<template>
<div>
<p>Prop 'initialValue': {{ initialValue }}</p>
<p>Local state 'currentValue': {{ currentValue }}</p>
<button @click="currentValue++">Increment Local Value</button>
<p>Window Inner Width: {{ windowSize.innerWidth }}px</p>
<p>Window Inner Height: {{ windowSize.innerHeight }}px</p>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import { updatablePropsEvenUnbound, windowSize } from 'vue-functions';
export default Vue.extend({
name: 'MyComponent',
// Use updatablePropsEvenUnbound to make 'initialValue' behave like v-model locally
mixins: [
updatablePropsEvenUnbound({
initialValue: { localName: 'currentValue' }, // Maps 'initialValue' prop to 'currentValue' data property
}),
windowSize, // Provides reactive windowSize object
],
props: {
initialValue: {
type: Number,
default: 10,
},
},
data() {
return {
// currentValue will be initialized from initialValue prop
// and can be updated locally without mutating the prop directly.
// windowSize is provided by the mixin and is reactive.
};
},
mounted() {
console.log('Component mounted. Initial local value:', (this as any).currentValue);
console.log('Window size available (innerWidth):', (this as any).windowSize.innerWidth);
},
watch: {
initialValue(newValue: number) {
console.log('Prop initialValue changed to:', newValue);
},
currentValue(newValue: number) {
console.log('Local currentValue changed to:', newValue);
// You can emit an event here if you want to update the parent component
// this.$emit('update:initialValue', newValue);
},
},
});
</script>