Giter Club home page Giter Club logo

vue-cookie's Introduction

vue-cookie2

A Vue.js plugin for manipulating cookies tested up to Vue v2.5.17

Installation

Install through npm or yarn:

# Use NPM
npm install vue-cookie2 --save
# Or  use yarn
yarn add vue-cookie2

Include in <body> after loading Vue and it will automatically hook into Vue:

<script src="/node_modules/vue-cookie/build/vue-cookie.js'"></script>

Or do it the cool way and load it in your main.js/app.js:

// Require dependencies
var Vue = require('vue');
var VueCookie = require('vue-cookie2');
// Tell Vue to use the plugin
Vue.use(VueCookie);

Usage

The plugin is available through this.$cookie in components or Vue.cookie.
Rather than implementing my own Cookie handling logic the plugin now wraps the awesome tiny-cookie package.

Example

// From some method in one of your Vue components
this.$cookie.set('test', 'Hello world!', 1);
// This will set a cookie with the name 'test' and the value 'Hello world!' that expires in one day

// To get the value of a cookie use
this.$cookie.get('test');

// To get all cookies
this.$cookie.getAll();

// To delete a cookie use
this.$cookie.remove('test');

// Flush all cookies
this.$cookie.flush();

Advanced examples

There are some options can be set in the third argument.

Domain

// Setting the cookie Domain
this.$cookie.set('test', 'Random value', {expires: 1, domain: 'localhost'});

// As this cookie is set with a domain then if you wish to delete it you have to provide the domain when calling delete
this.$cookie.remove('test', {domain: 'localhost'});

Expires

You can set the cookie expire time as you want.

var date = new Date;
date.setDate(date.getDate() + 21);

this.$cookie.set('dateObject', 'A date object', { expires: date });
this.$cookie.set('dateString', 'A parsable date string', { expires: date.toGMTString() });
this.$cookie.set('integer', 'Seven days later', { expires: 7 });
this.$cookie.set('stringSuffixY', 'One year later', { expires: '1Y' });
this.$cookie.set('stringSuffixM', 'One month later', { expires: '1M' });
this.$cookie.set('stringSuffixD', 'One day later', { expires: '1D' });
this.$cookie.set('stringSuffixh', 'One hour later', { expires: '1h' });
this.$cookie.set('stringSuffixm', 'Ten minutes later', { expires: '10m' });
this.$cookie.set('stringSuffixs', 'Thirty seconds later', { expires: '30s' });

Raw Data

We'll using encodeURIComponent/decodeURIComponent on set/get.
If you want to set/get raw data, you can use setRaw/getRaw method.

this.$cookie.setRaw('dateObject', 'A date object', { expires: date });
this.$cookie.getRaw('dataObject');

Thanks for using the plugin, I am happy to accept feedback/pull requests, do not forget to star if you like it!

Happy Coding! :D

Tests

This packacge uses the ´´´testemframework andjasmine``` assertion library

# Run npm install to fetch dependencies
yarn install

# Then you may run the tests from
yarn run test:dev

Acknowledgement

This plugin is based on alfhen/vue-cookie. Thank you for making the perfect plugin.

vue-cookie's People

Contributors

alfhen avatar vongola12324 avatar jackie19 avatar linroex avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.