• localStorage在不同页面之间的设置值与取值--加密 localStorage与解密localStorage


    在aa.vue页面

    <template>
      <div>
        <h1>在aa页面设置值</h1>
        <button @click="shezhi">用localstorage设置值localSt</button>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          list: [
            { id: 1, name: "张三" },
            { id: 2, name: "张三1" },
            { id: 3, name: "张三1" }
          ]
        };
      },
      methods: {
        shezhi() {
          // localStorage.setItem(“key”, JSON.stringify(obj));   // 存储对象  先转化
          // JSON.parse(window.localStorage.getItem(key))      //取对象  也要先转化
          window.localStorage.setItem("curName", JSON.stringify(this.list));
        }
      }
    };
    </script>
    
    <style lang="less" scoped>
    .el-main {
      line-height: 25px;
    }
    </style>

    在bb.vue

    <template>
      <div>
        <h1>获取aa页面的localstorage的值</h1>
        <button @click="quzhi">取值</button>
        <h1>下面是aa页面的值</h1>
    
        <p v-for="item in myarr" :key="item.id">
          <span>{{item.id}}-----{{item.name}}</span>
        </p>
    
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          myarr: []
        };
      },
    
      methods: {
        quzhi() {
          this.myarr = JSON.parse(window.localStorage.getItem("curName"));
          console.log(this.myarr); //有你值
        }
      }
    };
    </script>
    
    <style lang="less" scoped>
    .el-main {
      line-height: 25px;
    }
    </style>

    加密 localStorage与解密localStorage

    <template>
      <div>
        <h1>加密与解密</h1>
        <button @click="shezhi">加密localStorage</button>
    
        <button @click="jie">解密localStorage</button>获取值</div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          list: [
            { id: 1, name: "张三" },
            { id: 2, name: "张三1" },
            { id: 3, name: "张三1" }
          ]
        };
      },
      methods: {
        shezhi() {
          localStorage.setItem(
            "jiami",
            window.btoa(window.encodeURIComponent(JSON.stringify(this.list)))
          );
        },
    
        jie() {
          var obj = JSON.parse(
            decodeURIComponent(window.atob(localStorage.getItem("jiami")))
          );
          console.log(obj);
        }
      }
    
      // //用中文 记得加encodeURIComponent()!
      // localStorage.setItem("obj",window.btoa(window.encodeURIComponent(JSON.stringify(obj))));
    
      //用中文 记得加decodeURIComponent()!
      // var obj=JSON.parse(decodeURIComponent(window.atob(localStorage.getItem("obj"))));
      // console.info(obj);
    };
    </script>

  • 相关阅读:
    kali的一些基本操作
    Linux下find和rm的组合使用--整理转载
    虚拟接口模板- virtual-template
    点到点(point-to-point) 与 端到端(end to end)
    Ruby学习笔记-第二章
    Ruby学习笔记-第一章
    每天一个Linux命令-find
    每天一个Linux命令-du df
    每天一个Linux命令-more less
    每天一个Linux命令-cat
  • 原文地址:https://www.cnblogs.com/IwishIcould/p/11216133.html
Copyright © 2020-2023  润新知