Vue Mixins Collection
vue-mixins is a collection of reusable mixins designed for Vue.js applications, primarily targeting Vue 1.x and Vue 2.x versions. The package, currently at version 0.3.6 and last updated around 2020, includes utilities for DOM interaction (like `getViewportSize`, `onWindowResize`), event handling (`onClick`, `onDocument`), and state management (`isOpened`). Its policy states that mixins are never removed for deprecation; instead, if an API changes, the mixin's name is updated (e.g., `onResize` became `onResize2`). This reflects an older approach to code reuse in Vue, preceding the Composition API introduced in Vue 3. The package is effectively abandoned, lacking ongoing maintenance or compatibility updates for newer Vue versions.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES module (ESM) context without proper transpilation or configuration.fixEnsure your project or file uses CommonJS (`.js` files with `"type": "commonjs"` in `package.json` or older Node.js versions) or configure your build tool (e.g., Webpack) to resolve CJS modules in an ESM environment. Alternatively, include `bundle.js` in a browser for global access (`window.vueMixins`). -
[Vue warn]: The "mixins" option expects an array of mixin objects, but got undefined (for mixin at index X)
cause The `require()` path for a specific mixin is incorrect, or the module could not be found, resulting in `undefined` being passed to Vue's `mixins` option.fixDouble-check the exact path for the mixin (e.g., `vue-mixins/onClick`). Ensure the package is correctly installed and accessible at the specified path. This error can also occur if attempting to use with Vue 3, where mixins behavior is different and often discouraged in favor of Composition API. -
TypeError: Cannot read properties of undefined (reading 'call')
cause A mixin method (e.g., `this.onClick()`) is being called, but the mixin itself was not properly applied to the component, or the method was never defined by the mixin.fixVerify that the mixin is correctly listed in the component's `mixins` array and that the method you are calling is indeed provided by that mixin. Ensure you're using the correct mixin variant (e.g., `isOpened2` for Vue 2.x).
Warnings
- breaking This package is designed for Vue 1.x and Vue 2.x applications and is not compatible with Vue 3. Vue 3 introduced the Composition API as the preferred alternative to mixins for code reuse, and the underlying Vue APIs used by `vue-mixins` have changed significantly.
- breaking Several mixins have version-specific variants, such as `isOpened` (for Vue 1.x) and `isOpened2` (for Vue 2.0), or `parentListener` and `parentListener2`. Using the incorrect version for your Vue environment will lead to runtime errors or unexpected behavior.
- deprecated Some mixins are explicitly marked as deprecated in the README, such as `onResize` and `getVue`. While the package policy states that deprecated mixins are not removed, their functionality might be superseded by newer, renamed versions or other core Vue features.
- gotcha The package exclusively uses CommonJS `require` statements for modularity and offers a global `window.vueMixins` object when bundled. It does not provide ES module exports, making direct `import` statements in modern ESM-based projects problematic without specific build tool configurations (e.g., Webpack or Rollup handling CJS).
- gotcha This package is no longer actively maintained. The last release was several years ago, and the GitHub repository shows no recent activity. This means there will be no new features, bug fixes, or security patches.
Install
-
npm install vue-mixins -
yarn add vue-mixins -
pnpm add vue-mixins
Imports
- onClick
import { onClick } from 'vue-mixins'const onClickMixin = require('vue-mixins/onClick') - getViewportSize
import getViewportSize from 'vue-mixins/getViewportSize'
const getViewportSizeMixin = require('vue-mixins/getViewportSize') - isOpened2
const isOpenedMixin = require('vue-mixins/isOpened')const isOpened2Mixin = require('vue-mixins/isOpened2')
Quickstart
const Vue = require('vue');
const onClickMixin = require('vue-mixins/onClick');
const getViewportSizeMixin = require('vue-mixins/getViewportSize');
new Vue({
el: '#app',
mixins: [
onClickMixin,
getViewportSizeMixin
],
data: {
message: 'Hello from Vue Mixins!',
viewport: { width: 0, height: 0 }
},
methods: {
onClick() {
alert('Clicked! Viewport size: ' + JSON.stringify(this.getViewportSize()));
console.log('Viewport size:', this.getViewportSize());
}
},
created() {
// getViewportSize is provided by getViewportSizeMixin
this.viewport = this.getViewportSize();
console.log('Component created. Current viewport:', this.viewport);
window.addEventListener('resize', () => {
this.viewport = this.getViewportSize();
});
},
template: `
<div>
<h1>{{ message }}</h1>
<p>Current Viewport: {{ viewport.width }}px x {{ viewport.height }}px</p>
<button @click="click">Click me (via onClick mixin)</button>
<p>Open console to see mixin methods in action.</p>
</div>
`
});