简体   繁体   English

如何防止认证页面闪烁登录页面 VUEJS 3

[英]How to prevent authtenticated page flasshing the login page VUEJS 3

I am using vue3 can I ask for some help why is it redirecting to an authenticated page like home, when I refresh the login page will flash.我正在使用 vue3,请问为什么它会重定向到经过身份验证的页面,例如主页,当我刷新登录页面时会出现 flash。 every time I refresh it keeps flashing, how can I prevent this flashing login when I refresh the page or anywhere as long as I'm authenticated.每次刷新时它都会闪烁,只要我通过身份验证,当我刷新页面或任何地方时,如何防止这种闪烁的登录。

Thank you in advance.先感谢您。

// main.ts // main.ts

import { createApp } from 'vue'
import App from './App.vue'
import { store } from '@/vuex/store.js';

import router from './router';

const app = createApp(App)


app.use(store);
app.use(router)
app.mount('#app')

//router //路由器

import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '@/views/HomeView.vue'

import Login from '@/components/Login.vue'

// @ts-ignore
import {store} from "@/vuex/store";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: Login,

        },
        {
            path: '/home',
            name: 'home',
            component: HomeView
        }

    ]
})

router.beforeEach((to, from, next) => {

 next()
})

export default router

// login.vue // 登录.vue

<template>
   <div>
   <h1>Login </h1>
      <form action="#" method="post">
          <div class="input-group mb-3">
            <input type="text" id="username" v-model="username" class="form-control" placeholder="Username">
            <div class="input-group-append">
              <div class="input-group-text">
                <span class="fas fa-envelope"></span>
              </div>
            </div>
          </div>
          <div class="input-group mb-3">
            <input type="password" id="password" v-model="password" class="form-control" placeholder="Password">
            <div class="input-group-append">
              <div class="input-group-text">
                <span class="fas fa-lock"></span>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-8">
              <div class="icheck-primary">
                <input type="checkbox" id="remember">
                <label for="remember">
                  Remember Me
                </label>
              </div>
            </div>
            <!-- /.col -->
            <div class="col-4">
              <button type="submit" class="btn btn-primary btn-block" @click.prevent="login">Sign In</button>
            </div>
            <!-- /.col -->
          </div>
        </form>
   </div>
</template>

<script setup>
import {ref} from 'vue';
import {useStore} from 'vuex'
import {useRouter} from 'vue-router';


const username = ref('');
const password = ref('');

const store = useStore();
const router = useRouter();


const login = async () => {

  await store.dispatch('fetchToken', {username: username.value, password: password.value});
  await store.dispatch('fetchUserLogin');
  await router.push({name: 'home'});
}


</script>

// App.vue // 应用程序.vue

<template>

  <router-view v-if="store.state.isLogin"/>
  <login v-else></login>

</template>

<script setup lang="ts">
import Login from '@/components/Login.vue'

import {useStore} from 'vuex';
import {RouterView} from "vue-router";
import {onBeforeMount, onMounted, ref} from "vue";

const store = useStore();


onMounted(async ()=>{
  await store.dispatch('fetchUserLogin')
});


</script>
<router-view v-if="store.state.isLogin"/>
<login v-else></login>

while store.state.isLogin is false, the page will show the Login component.store.state.isLogin为 false 时,页面将显示 Login 组件。 store.state.isLogin is always initially false until this asychronous operation completes: store.state.isLogin最初始终为 false,直到此异步操作完成:

await store.dispatch('fetchUserLogin')

That's why you always see a flash of the login component.这就是为什么您总是看到登录组件的 flash 的原因。 store.state.isLogin is false until the dispatch is complete, which takes some small amount of time. store.state.isLogin在调度完成之前为,这需要一些时间。

There are multiple ways you could handle this, one would be to have a isLoading property that is initially true and is then set to false once the fetchUserLogin is done.有多种方法可以处理这个问题,一种是拥有一个isLoading属性,该属性最初为 true,然后在fetchUserLogin完成后设置为 false。 You can use isLoading to choose to not render anything (or render some loading modal) while the fetching is ongoing:您可以使用isLoading选择在获取过程中不渲染任何内容(或渲染某些加载模式):

<div v-if="isLoading"></div>
<div v-else>
  <router-view v-if="store.state.isLogin"/>
  <login v-else></login>
</div>
const store = useStore();
const isLoading = true;

onMounted(async () => {
  await store.dispatch('fetchUserLogin');
  isLoading = false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM