All Projects → anish2690 → vue-cookie-next

anish2690 / vue-cookie-next

Licence: MIT License
A vue 3 plugin for handling browser cookies with typescript support. Load and save cookies within your Vue 3 application

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to vue-cookie-next

Iconpark
🍎Transform an SVG icon into multiple themes, and generate React icons,Vue icons,svg icons
Stars: ✭ 4,924 (+13208.11%)
Mutual labels:  vue3, vue-next
Vueuse
Collection of essential Vue Composition Utilities for Vue 2 and 3
Stars: ✭ 7,290 (+19602.7%)
Mutual labels:  vue3, vue-next
vue3.0-template-admin
本项目基于vue3+ElementPlus+Typescript+Vite搭建一套通用的后台管理模板;并基于常见业务场景,抽象出常见功能组件;包括动态菜单,菜单权限、登录、主题切换、国际化、个人中心、表单页、列表页、复制文本、二维码分享等等
Stars: ✭ 500 (+1251.35%)
Mutual labels:  vue3, vue-next
vue3-analysis
vue3源码解释😃
Stars: ✭ 120 (+224.32%)
Mutual labels:  vue3, vue-next
bpmn-vue-activiti
基于Vue3.x + Vite + bpmn-js + element-plus + tsx 实现的Activiti流程设计器(Activiti process designer based on Vue3.x + Vite + BPMN-JS + Element-Plus + TSX implementation)
Stars: ✭ 345 (+832.43%)
Mutual labels:  vue3, vue-next
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (+40.54%)
Mutual labels:  vue3, vue-next
codemirror-editor-vue3
CodeMirror component for Vue3
Stars: ✭ 22 (-40.54%)
Mutual labels:  vue3, vue-next
vue3-jd-h5
🔥 Based on vue3.0.0, vant3.0.0, vue-router v4.0.0-0, vuex^4.0.0-0, vue-cli3, mockjs, imitating Jingdong Taobao, mobile H5 e-commerce platform! 基于vue3.0.0 ,vant3.0.0,vue-router v4.0.0-0, vuex^4.0.0-0,vue-cli3,mockjs,仿京东淘宝的,移动端H5电商平台!
Stars: ✭ 660 (+1683.78%)
Mutual labels:  vue3, vue-next
vue-next-rx
RxJS integration for Vue next
Stars: ✭ 31 (-16.22%)
Mutual labels:  vue3, vue-next
vue-data-visualization
基于Vue3.0的“数据可视化大屏”设计与编辑器
Stars: ✭ 84 (+127.03%)
Mutual labels:  vue3, vue-next
vue3-spring
A spring-physics based animation library, and more
Stars: ✭ 30 (-18.92%)
Mutual labels:  vue3
Zebra Cookie
A ridiculously small (~500 bytes minified) JavaScript API for writing, reading and deleting browser cookies
Stars: ✭ 15 (-59.46%)
Mutual labels:  cookie
mini-vue-devui
村长和kagol直播节目《我要做开源》产出的开源组件库教学项目
Stars: ✭ 42 (+13.51%)
Mutual labels:  vue3
vue3-boilerplate
A Vue 3 Starter Boilerplate with Vue Router 4, Vuex 4, TypeScript 4, Webpack 5, Prettier and More.
Stars: ✭ 133 (+259.46%)
Mutual labels:  vue3
ItroublveTSC
Official Source of ItroublveTSC, totally open source. No virus or anything. Feel free to have a look :)
Stars: ✭ 82 (+121.62%)
Mutual labels:  cookie
svelte-persistent-store
A Svelte store that keep its value through pages and reloads
Stars: ✭ 111 (+200%)
Mutual labels:  cookie
monoreact
📦 React workspaces implementation
Stars: ✭ 13 (-64.86%)
Mutual labels:  typescript-library
bootstrap-vue-3
Early (but lovely) implementation of Vue 3, Bootstrap 5 and Typescript
Stars: ✭ 314 (+748.65%)
Mutual labels:  vue3
ng2-rest
Isomorphic, simple, robust REST API library for Browser and NodeJS, ( ts / js ) apps
Stars: ✭ 26 (-29.73%)
Mutual labels:  typescript-library
vuepress-theme-gungnir
A blog theme for VuePress 2.
Stars: ✭ 160 (+332.43%)
Mutual labels:  vue3

vue-cookie-next

A simple Vue 3 plugin for handling browser cookies with typescript support

Installation

Browser

<html lang="en">
  <head>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app"></div>
  </body>
  <script type="module">
    import { VueCookieNext } from 'https://unpkg.com/[email protected]/dist/vue-cookie-next.esm-bundler.js'
    const CookieTest = {
      mounted() {
        this.$cookie.setCookie('username', 'user1')
        console.log(this.$cookie.getCookie('username'))
      },
    }
    Vue.createApp(CookieTest).use(VueCookieNext).mount('#app')
  </script>
</html>

Package Managers

npm install vue-cookie-next
//or
yarn add vue-cookie-next
import { createApp } from 'vue'
import { VueCookieNext } from 'vue-cookie-next'

import App from 'App.vue'
const app = createApp(App)
app.use(VueCookieNext)
app.mount('#app')

// set default config
VueCookieNext.config({ expire: '7d' })

// set global cookie
VueCookieNext.setCookie('theme', 'default')
VueCookieNext.setCookie('hover-time', { expire: '1s' })

Composition API

import { defineComponent } from 'vue'
import { useCookie } from 'vue-cookie-next'

defineComponent({
  setup() {
    const { setCookie, removeCookie } = useCookie()
    setCookie('theme', 'dark')
    removeCookie('hover-time')
  },
})

API Options

syntax format: [this | VueCookieNext].$cookie.[method]

  • Set global config
VueCookieNext.config({
  expire: '1d',
  path: '/',
  domain: '',
  secure: '',
  sameSite: '',
})
// default: expireTimes = 1d, path = '/', domain = '', secure = '', sameSite = 'Lax'
  • Set a cookie
this.$cookie.setCookie(keyName, value, {
  expire: '1d',
  path: '/',
  domain: '',
  secure: '',
  sameSite: '',
}) //return this
  • Get a cookie
this.$cookie.getCookie(keyName) // return value
  • Remove a cookie
this.$cookie.removeCookie(keyName, {
  path: '/',
  domain: '',
}) // return this | false if key not found
  • Exist a cookie name
this.$cookie.isCookieAvailable(keyName) // return false or true
  • Get All cookie name
this.$cookie.keys() // return a array string

Example Usage

set global config

import { VueCookieNext } from 'vue-cookie-next'
// 30 day after, expire
VueCookieNext.config({ expire: '30d' })

// set secure, only https works
VueCookieNext.config({ expire: '7d', secure: true })

// 2019-03-13 expire
VueCookieNext.config({ expire: new Date(2019, 03, 13).toUTCString() })

// 30 day after, expire, '' current path , browser default
VueCookieNext.config({ expire: 60 * 60 * 24 * 30 })

support json object

var user = {
  user_id: 1,
  name: 'Ben',
  session: '75442486-0878-440c-9db1-a7006c25a39f',
  session_start_time: new Date(),
}

this.$cookie.setCookie('user', user)
// print user name
console.log(this.$cookie.getCookieCookie('user').name)

set expire times

Suppose the current time is : Sat, 11 Mar 2017 12:25:57 GMT

Following equivalence: 1 day after, expire

Support chaining sets together

// default expire time: 1 day
this.$cookie
  .setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f')
  // number + d , ignore case
  .setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
    expire: '1d',
  })
  .setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
    expire: '1D',
  })
  // Base of second
  .setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
    expire: 60 * 60 * 24,
  })
  // input a Date, + 1day
  .setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
    expire: new Date(2017, 03, 12),
  })
  // input a date string, + 1day
  .setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
    expire: 'Sat, 13 Mar 2017 12:25:57 GMT',
  })

set expire times, input number type

this.$cookie.setCookie('default_unit_second', 'input_value', { expire: 1 }) // 1 second after, expire
this.$cookie.setCookie('default_unit_second', 'input_value', {
  expire: 60 + 30,
}) // 1 minute 30 second after, expire
this.$cookie.setCookie('default_unit_second', 'input_value', {
  expire: 60 * 60 * 12,
}) // 12 hour after, expire
this.$cookie.setCookie('default_unit_second', 'input_value', {
  expire: 60 * 60 * 24 * 30,
}) // 1 month after, expire

set expire times - end of browser session

this.$cookie.setCookie('default_unit_second', 'input_value', { expire: 0 }) // end of session - use 0 or "0"!

set expire times , input string type

Unit full name
y year
m month
d day
h hour
min minute
s second

Unit Names Ignore Case

not support the combination

not support the double value

this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: '60s',
}) // 60 second after, expire
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: '30MIN',
}) // 30 minute after, expire, ignore case
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: '24d',
}) // 24 day after, expire
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: '4m',
}) // 4 month after, expire
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: '16h',
}) // 16 hour after, expire
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: '3y',
}) // 3 year after, expire

// input date string
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: new Date(2017, 3, 13).toUTCString(),
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: 'Sat, 13 Mar 2017 12:25:57 GMT ',
})

set expire support date

var date = new Date()
date.setDate(date.getDate() + 1)
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: date,
})

set never expire

this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
  expire: Infinity,
}) // never expire
// never expire , only -1,Other negative Numbers are invalid
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', { expire: -1 })

remove cookie

this.$cookie.setCookie('token', 'value') // domain.com and *.doamin.com are readable
this.$cookie.removeCookie('token') // remove token of domain.com and *.doamin.com

this.$cookie.setCookie('token', value, { domain: 'domain.com' }) // only domain.com are readable
this.$cookie.removeCookie('token', { domain: 'domain.com' }) // remove token of domain.com

set other arguments

// set path
this.$cookie.setCookie('use_path_argument', 'value', {
  expire: '1d',
  path: '/app',
})

// set domain
this.$cookie.setCookie('use_path_argument', 'value', { domain: 'domain.com' }) // default 1 day after,expire

// set secure
this.$cookie.setCookie('use_path_argument', 'value', {
  secure: true,
})

// set sameSite - should be one of `None`, `Strict` or `Lax`. Read more https://web.dev/samesite-cookies-explained/
this.$cookie.setCookie('use_path_argument', 'value', { sameSite: 'Lax' })

other operation

// check a cookie exist
this.$cookie.isCookieAvailable("user_session")

// get a cookie
this.$cookie.getCookie("user_session");

// remove a cookie
this.$cookie.removeCookie("user_session");

// get all cookie key names, line shows
this.$cookie.keys().join("\n");

// remove all cookie
this.$cookie.keys().forEach(cookie => this.$cookie.removeCookie(cookie))

// vue-cookie-next global
[this | VueCookieNext].$cookie.[method]

⚠️ Warning

$cookie key names Cannot be set to ['expires','max-age','path','domain','secure','SameSite']

🌸 Thanks

This project is heavily inspired by the following awesome projects.

Thanks!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].