<template>
  <div id="app">
    <!--实现路由切换动画-->
    <transition :name="transitionName">
      <keep-alive include="StoreList">
        <RouterView v-if="isRouterAlive" />
      </keep-alive>

      <!-- <keep-alive>
        <router-view v-if="$route.meta.keepAlive"></router-view>
      </keep-alive>
      <router-view v-if="!$route.meta.keepAlive"></router-view> -->
    </transition>
  </div>
</template>
<script>
export default {
  data() {
    return {
      transitionName: '',
      isRouterAlive: true
    }
  },
  provide() {
    return {
      reload: this.reload
    }
  },
  created () {
    // 在页面加载时读取sessionStorage
    // 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))
    // })
    //ios刷新时vuex信息保留
    let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isiOS) {
      //在页面刷新时将vuex里的信息保存到缓存里
      window.addEventListener("pagehide", () => {
        localStorage.setItem("store", JSON.stringify(this.$store.state))
      })
      //在页面加载时读取localStorage里的状态信息
      localStorage.getItem("store") && this.$store.replaceState(Object.assign(this.$store.state,JSON.parse(localStorage.getItem("store"))));
    } else {
      //在页面刷新时将vuex里的信息保存到缓存里
      window.addEventListener("beforeunload", () => {
        localStorage.setItem("store", JSON.stringify(this.$store.state))
      })
      //在页面加载时读取localStorage里的状态信息
      localStorage.getItem("store") && this.$store.replaceState(Object.assign(this.$store.state,JSON.parse(localStorage.getItem("store"))));
    }
  },
  mounted(){
  },
  methods: {
    reload() {
      this.isRouterAlive = false
      this.$nextTick(() => {
        this.isRouterAlive = true
      })
    }
  }
  // watch: {
  //   $route(to, from) {
  //     //首次加载去除动画
  //     if (!from.name) return
  //     //动画方式
  //     if (to.meta.index > from.meta.index) {
  //       this.transitionName = 'slide-left'
  //     } else if (to.meta.index === from.meta.index) {
  //       this.transitionName = ''
  //     } else {
  //       this.transitionName = 'slide-right'
  //     }
  //   }
  // }
}
</script>

<style lang="scss">
@import '@/assets/sass/common.scss';
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  height: 100%;
}
.slide-left-leave-active,
.slide-left-enter-active,
.slide-right-leave-active,
.slide-right-enter-active {
  position: absolute !important;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 99;
  transition: all 0.2s ease;
}
.slide-left-enter,
.slide-right-leave-to {
  transform: translateX(100%);
}
.slide-left-enter-to,
.slide-left-leave,
.slide-right-enter-to,
.slide-right-leave {
  transform: translateX(0);
}
.slide-left-leave-to,
.slide-right-enter {
  transform: translateX(-100%);
}
</style>
