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