0.1.8 • Published 7 years ago

cookie-in-vue v0.1.8

Weekly downloads
16
License
MIT
Repository
github
Last release
7 years ago

Cookie-in-vue

This is a vue plugin to oparate cookie,and it depend js-cookie

Installation

npm install cookie-in-vue --save

Usage

import Vue from 'vue'
import VueCookie from 'cookie-in-vue'

Vue.use(VueCookie)

// somo.vue
export default new Vue({
  // ...
  methods: {
    setCookies(token) {
      this.$cookie.set('token', token, 7, 'localhost')
    },
    getSomeCookies() {
      this.$cookie.get('token')
    },
    removeSomeCookies(name) {
      this.$cookie.remove('token')
    }
  }
})

// other ways
VueCookie.set('token', token, 7, 'localhost')
VueCookie.get('token')
VueCookie.remove('token') // VueCookie.remove('token', -1, 'localhost')

All code

(function () {

  const Cookies = require('js-cookie')

  Number.isInteger = Number.isInteger || function(value) {
      return typeof value === 'number' && isFinite(value) &&
        Math.floor(value) === value
    }

  const VueCookie = {
    install(Vue, option) {
      Vue.prototype.$cookie = this
      Vue.cookie = this
    },
    set(name, value, expires, path) {
        expires = expires || 7
      if (Number.isInteger(expires)) {
        if(expires > 0) {
          return Cookies.set(name, value, {expires: expires, path: path || '/'})
        }
      } else {
        console.error('Expires in VueCookie: Expected an integer value')
      }

    },
    get(name) {
      return Cookies.get(name)
    },
    remove(name, path) {
      this.set(name, '', -1, path)
    }
  }
  if (typeof exports == "object") {
    module.exports = VueCookie;
  } else if (typeof define == "function" && define.amd) {
    define([], function(){ return VueCookie; })
  } else if (window.Vue) {
    window.VueCookie = VueCookie;
    Vue.use(VueCookie);
  }
})()
0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago