Directly manipulate cookies in Vue and how to use tools Js-cookie

Source: Internet
Author: User

Directly manipulate cookies in Vue

The following 3 modes of operation

Set: function (name, value, days) {varD =NewDate; D.settime (D.gettime ()+ -* -* -* +*Days ); Window.document.cookie= name +"="+ Value +";p ath=/;expires="+d.togmtstring ();},Get: function (name) {varv = window.document.cookie.match ('(^|;) ?'+ name +'=([^;] *)(;|$)'); returnV? v[2] :NULL;},delete:function (name) { This.Set(Name,"', -1);}

Using the Js-cookie tool: (more convenient, recommended use)

Tool Address: Https://www.npmjs.com/package/js-cookie

  cnpm i Js-cookie
Introduced
from ' Js-cookie '

Use

For specific use, see the following:

Basic usagecreate A cookie, valid across the entire site:cookies.Set('name','value'); Create a cookie that expires7Days fromNow , valid across the entire site:cookies.Set('name','value', {expires:7 }); Create an expiring cookie with valid to the path of the current page:cookies.Set('name','value', {expires:7, Path:"' }); Read cookie:cookies.Get('name');//= ' value 'Cookies.Get(' Nothing');//= undefinedRead all visible cookies:cookies.Get();//= = {Name: ' Value '}Delete Cookie:Cookies.remove ('name');D elete A cookie valid to the path of the current page:cookies.Set('name','value', {path:"' }); Cookies.remove ('name');//fail!Cookies.remove ('name', {path:"'});//removed!important! When deleting a cookies, you must pass the exact same path and domain attributes, is used toSetThe cookie, unless you're relying on the default attributes.note:removing unexisting Cookie does not raise any exception norreturnAny value

Directly manipulate cookies in Vue and how to use tools Js-cookie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.