你好老师,这里导出为什么会报错?
Default={
maxAge:undefined,
domain:undefined,
path:undefined,
secure:undefined
}
Default2={
maxAge:-1,
domain:undefined,
path:undefined
}
class CookieFun{
constructor(name,value,options=Default){
this.name=name;
this.value=value;
this.options=options;
this.set();
// this.get();
// this.remove();
}
set(){
let cookieText=`${encodeURIComponent(this.name)}=${encodeURIComponent(this.value)}`;
if(typeof this.options.maxAge==='number'){
cookieText+=`; max-age=${this.options.maxAge}`;
}
if(this.options.domain){
cookieText+=`; domain=${this.options.domain}`;
}
if(this.options.path){
cookieText+=`; path=${this.options.path}`;
}
if(this.options.secure){
cookieText+=`; secure`;
}
document.cookie=cookieText;
}
get(){
this.name=`${encodeURIComponent(this.name)}`;
const cookies=document.cookie.split('; ');
for (const item of cookies) {
const [cookieName,cookieValue]=item.split('=')
if(cookieName===this.name){
// console.log(`${decodeURIComponent(cookieValue)}`);
return `${decodeURIComponent(cookieValue)}`;
}
}
return undefined;
}
remove(){
this.set(this.name,'',this.options=Default2)
}
}
export{CookieFun,Default,Default2}试着用class封装cookie,这样写对不对,remove方法该怎么正确设置参数,现在的remove自己测试了一下好像有点问题。
为什么我get方法变成static静态方法无法这样调用呢 CookieFun.get()?
然后最后导出时,默认值报错?不知道怎么改。
13
收起
正在回答
1回答
同学你好,同学描述的问题老师也没办法得知,如可以同学需要将涉及到的代码全部贴上来,另外包括项目目录结构一并贴上来,老师这边试着帮同学测试一下看看。
祝学习愉快!



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星