Here are a few cookie methods that I simply encapsulated.

Utils.ts

/** * Custom public functions */
const utils = 
{
/ * * *@name: Sets the cookie value *@author: camellia
     * @date: 2020-12-28 
     * @param: cname string Cookie name *@param: cvalue any Cookie value *@param: exdays number Number of days for storing cookies */
    setCookie(cname:string, cvalue:any, exdays = 720) 
    {
        var d = new Date(a); d.setTime(d.getTime() + (exdays *24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    },
    / * * *@name: Gets the cookie value *@author: camellia
     * @date: the 2020-12-28 * /
    getCookie(cname:string) {
        var name = cname + "=";
        var ca = document.cookie.split('; ');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) = =' ') c = c.substring(1);
            if(c.indexOf(name) ! = -1) {
                returnc.substring(name.length, c.length); }}return "";
    },
    / * * *@name: Clears cookie value *@author: camellia
     * @date: 2020-12-28 
     * @param: cname string Cookie name */
    clearCookie(cname:string) {
        var d = new Date(a); d.setTime(-1);
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "= ' '; "+ expires; }},export default utils;
Copy the code

Call:

// Import the common JS file
import utils from "utils.js";
/ / set the cookieUtils. SetCookie (' index ',1);
/ / get a cookieUtils. GetCookie (" index ");/ / set the cookieUtils. ClearCookie (" index ");Copy the code

For good suggestions, please enter your comments below.

Welcome to guanchao.site

Welcome to applet: