<template></template>

<script lang="ts">
import { defineComponent, onMounted } from "vue";
import {
  NDialogProvider,
  NNotificationProvider,
  NMessageProvider,
  useDialog,
} from "naive-ui";
import { useRouter } from "vue-router";

export default defineComponent({
  name: "RouterError",
  setup() {
    const router = useRouter();
    const dialog = useDialog();
    let isOpen = false;
    router.onError((error) => {
      console.log(typeof error);
      console.log(error.message, "error");
      let str = error.message + "";
      const isChunkLoadFailed = str.indexOf("imported");

      // const targetPath = router.history.pending.fullPath
      if (isChunkLoadFailed != -1 && !isOpen) {
        isOpen = true;
        dialog.warning({
          title: "更新提示",
          content: "网站有更新请点击确定刷新页面?",
          positiveText: "确定",
          negativeText: "取消",
          onPositiveClick: () => {
            // location.hash = targetPath
            window.location.reload();
          },
          onNegativeClick: () => {},
        });
      }
    });

    return {};
  },
});
</script>
