• 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>

  • 相关阅读:
    shell script-条件语句、循环语句
    shell script-判断式
    shell script
    AngularJs(Part 11)--自定义Directive
    AngularJS中的DI
    javascript的DI
    一些奇怪的Javascript用法
    AngularJS(Part 10)--页面导航
    URL中#符号的作用
    AngularJs(Part 9)--AngularJS 表单
  • 原文地址:https://www.cnblogs.com/IwishIcould/p/11216133.html
Copyright © 2020-2023  润新知