zoukankan      html  css  js  c++  java
  • 用sessionStorage实现页面之间的数据传输

    1、sessionStorage主要含几种方法:
      //页面A:存放一个简单的字符串
      sessionStorage.obj = '123';
      //页面B:取到给obj
      var str = sessionStorage.obj;
      //类型地:
      sessionStorage.setItem(key,value);
      sessionStorage.gettItem(key,value);
      sessionStorage.remove(key);
    2、对于常用的字段传输,是没问题的,但是对于以下情况:
      //存放对象、数组
      var obj = { name:'Jim' };
      sessionStorage.obj = obj;
      localStorage.obj = obj;

      var arr = [1,2,3];
      sessionStorage.obj = arr;
      localStorage.obj = arr;
      //读取是不行的,这里应该在存放对象和数组之前,通过JSON对象提供的parse和stringify将其他数据类型转化成字符串,再存储到storage中。
      例如:
      var str = JSON.stringify(vim.todos[index]);
      //存入
      sessionStorage.setItem('newsObject',str);
      //存入记录当前页面,以便从详情页面返回时使用
      sessionStorage.setItem('currentPage',currentPage);
      sessionStorage.setItem('currentPage2',currentPage2);
      //读取
      var newsObject = sessionStorage.getItem('newsObject');
      //重新转换为对象
      newsObject = JSON.parse(newsObject);
      alert(newsObject.title);

     
  • 相关阅读:
    ngInclude与script加载模板
    ng-model-options
    angular模板加载 ----ng-template
    ngBind ngBindTemplate ngBindHtml
    ng-switch
    ng-show与ng-if区别
    运维教给我什么
    路漫漫其修远兮,吾将上下而求索
    那些让我们继续坚持下去句子
    随手记
  • 原文地址:https://www.cnblogs.com/chq3272991/p/5647453.html
Copyright © 2011-2022 走看看