分享

成功解决vue store页面刷新数据丢失问题

 北方天空A 2020-09-15

<template>
  <div :class="{'has-logo':showLogo}">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <div style="height:100px;font-size:12px;color:white;text-align:left" v-show="isShow">
      <div>
        卵障号:
        <label>{{this.LZBaseSession.EggDevelopementDisorderNumber}}</label>
      </div>
      <div style="margin-top:10px;">
        家系号:
        <label>{{this.LZBaseSession.EggDevelopementDisorderNumber}}</label>
      </div>
      <div style="margin-top:10px;">医卡通号:</div>
      <div style="margin-top:10px; ">
        辅助生殖号:
        <!-- <a style="width:17px;float:right;"><i class="el-icon-setting"></i></a> -->
      </div>
    </div>
    <el-scrollbar
      id="scrollbar"
      wrap-class="scrollbar-wrapper"
      style="position: fixed; top: 160px; bottom: 0; width: 210px; height: unset;"
    >
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :unique-opened="false"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        mode="vertical"
      >
        <sidebar-item
          v-for="route in routesToShow"
          :key="route.path"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Logo from "./Logo";
import SidebarItem from "./SidebarItem";
import variables from "@/styles/variables.scss";
import store from "@/store";
// import variables from "@/src/variables.scss";

export default {
  components: { SidebarItem, Logo },
  data() {
    return {
      logo: require("@/icons/logo.jpg"),
      isShow: true,
    };
  },
  created() {
    // 在页面加载时读取sessionStorage
    if (!this.OperateModel) {
      if (sessionStorage.getItem("store")) {
        this.$store.replaceState(
          Object.assign(
            {},
            this.$store.state,
            JSON.parse(sessionStorage.getItem("store"))
          )
        );
      }
    }
    // 在页面刷新时将store保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("store", JSON.stringify(this.$store.state));
    });
  },
  mounted() {
    this.ini();
  },
  methods: {
    ini() {
      var OperateModel = this.OperateModel;
      if (OperateModel == "当前病例") {
        this.isShow = true;
        document.getElementById("scrollbar").style.top = "160px";
      } else {
        this.isShow = false;
        document.getElementById("scrollbar").style.top = "80px";
      }
    },
  },
  computed: {
    ...mapGetters([
      "permission_routes",
      "sidebar",
      "LZBaseSession",
      "OperateModel",
    ]),

   
    activeMenu() {
      const route = this.$route;
      const { meta, path } = route;
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu;
      }
      return path;
    },

    showLogo() {
      return this.$store.state.settings.sidebarLogo;
    },
    variables() {
      return variables;
    },
    isCollapse() {
      return !this.sidebar.opened;
    },
  },
};
</script>
主要是在相关页面created事件中加上代码:
// 在页面加载时读取sessionStorage
    if (!this.OperateModel) {
      if (sessionStorage.getItem("store")) {
        this.$store.replaceState(
          Object.assign(
            {},
            this.$store.state,
            JSON.parse(sessionStorage.getItem("store"))
          )
        );
      }
    }
    // 在页面刷新时将store保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("store", JSON.stringify(this.$store.state));
    });
Store patient.js文件
const state = {
    name: 'hahhaxx',
    LZBaseSession: [],
    OperateModel:'',//当前操作,主要用于识别顶部导航样本入库和统计查询
}

const mutations = {
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_LZBase: (state, LZBaseSession) => {
        state.LZBaseSession = LZBaseSession;
    },
    SET_OperateModel: (state, OperateModel) => {
        state.OperateModel = OperateModel;
    }
}

export default {
    namespaced: true,
    state,
    mutations
}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多