import { useDialog } from 'naive-ui';
import { defineComponent } from 'vue';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: 'router-error',
  setup() {
    const router = useRouter();
    const dialog = useDialog();
    let isOpen = false;
    router.onError(error => {
      const 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: () => {
            window.location.reload();
          }
        });
      }
    });
    return () => <div></div>;
  }
});
