用sessionStorage實(shí)現(xiàn)頁面之間的數(shù)據(jù)傳輸
1、sessionStorage主要含幾種方法:
//頁面A:存放一個(gè)簡單的字符串
sessionStorage.obj = '123';
//頁面B:取到給obj
var str = sessionStorage.obj;
//類型地:
sessionStorage.setItem(key,value);
sessionStorage.gettItem(key,value);
sessionStorage.remove(key);
2、對于常用的字段傳輸逗宁,是沒問題的节腐,但是對于以下情況:
//存放對象、數(shù)組
var obj = { name:'Jim' };
sessionStorage.obj = obj;
localStorage.obj = obj;
var arr = [1,2,3];
sessionStorage.obj = arr;
localStorage.obj = arr;
//讀取是不行的,這里應(yīng)該在存放對象和數(shù)組之前涡相,通過JSON對象提供的parse和stringify將其他數(shù)據(jù)類型轉(zhuǎn)化成字符串适袜,再存儲到storage中柄错。
例如:
var str = JSON.stringify(vim.todos[index]);
//存入
sessionStorage.setItem('newsObject',str);
//存入記錄當(dāng)前頁面,以便從詳情頁面返回時(shí)使用
sessionStorage.setItem('currentPage',currentPage);
sessionStorage.setItem('currentPage2',currentPage2);
//讀取
var newsObject = sessionStorage.getItem('newsObject');
//重新轉(zhuǎn)換為對象
newsObject = JSON.parse(newsObject);
alert(newsObject.title);
轉(zhuǎn)載自https://www.cnblogs.com/chq3272991/p/5647453.html
親測非常好用苦酱!