Vue Cookie Consent Component
vue-cookieconsent-component is a straightforward Vue 2 component designed to display a cookie consent banner, helping websites comply with cookie regulations. Its primary function is to show a configurable message, a 'Got it!' button, and a 'Learn more' link, saving the user's consent status in a cookie. The current stable version is 1.2.0, released in 2017. As the project has seen no significant updates or new releases since then, and given its specific compatibility with Vue 2 (which is now in maintenance/end-of-life status), it is considered abandoned. It offers basic customization through props and slots for message, link, and button content, and allows styling overrides via Sass variables. It differentiates itself as a simple, unopinionated wrapper around basic cookie consent functionality, inspired by `insites/cookieconsent` (v2), without complex features like geolocation or multiple consent types.
Common errors
-
Failed to compile component: Unknown custom element <cookie-consent> - did you register the component correctly?
cause The `cookie-consent` component has not been registered either globally or locally within the Vue application where it's being used.fixGlobally register the component using `Vue.component('cookie-consent', cookieconsent)` in your main application entry file (e.g., `main.js`), or locally register it in the `components` option of the parent Vue component: `export default { components: { CookieConsent } }`. -
Module not found: Error: Can't resolve './node_modules/vue-cookieconsent-component/src/scss/cookie-consent' in '...' or similar CSS import errors.
cause The Sass `@import` path for the component's styles is incorrect or your build system (e.g., webpack's `sass-loader`) is not configured to resolve paths starting with `./node_modules` correctly.fixChange the `@import` statement to use the `~` alias for `node_modules`: `@import '~vue-cookieconsent-component/src/scss/cookie-consent';`. This is a common convention that build tools like Webpack understand for resolving packages. If `~` doesn't work, verify your webpack configuration for `sass-loader` or `css-loader`.
Warnings
- breaking This component is designed specifically for Vue 2 applications. It is not compatible with Vue 3 due to fundamental API changes in Vue's component system. Attempting to use it in a Vue 3 project will result in runtime errors.
- gotcha The project is no longer actively maintained. The last release (v1.2.0) was in 2017, and there has been minimal activity on the GitHub repository since. This means there will be no future updates, bug fixes, or security patches.
- gotcha Importing the component's SCSS styles directly from `node_modules` requires a specific path. If your build system (e.g., webpack with sass-loader) does not resolve `~` to `node_modules`, the `@import` statements will fail.
Install
-
npm install vue-cookieconsent-component -
yarn add vue-cookieconsent-component -
pnpm add vue-cookieconsent-component
Imports
- cookieconsent
const cookieconsent = require('vue-cookieconsent-component')import cookieconsent from 'vue-cookieconsent-component'
- CookieConsent
import CookieConsent from 'vue-cookieconsent-component'
- Vue.component
Vue.component('cookieconsent', cookieconsent)import Vue from 'vue' import cookieconsent from 'vue-cookieconsent-component' Vue.component('cookie-consent', cookieconsent)
Quickstart
import Vue from 'vue';
import App from './App.vue';
import cookieconsent from 'vue-cookieconsent-component';
// Register the component globally
Vue.component('cookie-consent', cookieconsent);
new Vue({
render: h => h(App),
}).$mount('#app');
// In your App.vue or any other Vue component's template:
// <template>
// <div id="app">
// <!-- Other content -->
// <cookie-consent
// message="Our website uses cookies to improve your experience."
// button-label="Got it!"
// link-label="Read our privacy policy"
// href="/privacy-policy"
// target="_self"
// />
// </div>
// </template>
// To include the recommended CSS (e.g., in your main.scss or App.vue style block):
// @import '~vue-cookieconsent-component/src/scss/cookie-consent';
// @import '~vue-cookieconsent-component/src/scss/cookie-consent-bottom';
// @import '~vue-cookieconsent-component/src/scss/cookie-consent-transition';